@@ -233,6 +233,39 @@ static struct btf_id *add_symbol(struct rb_root *root, char *name, size_t size)
233
233
return btf_id__add (root , id , false);
234
234
}
235
235
236
+ /*
237
+ * The data of compressed section should be aligned to 4
238
+ * (for 32bit) or 8 (for 64 bit) bytes. The binutils ld
239
+ * sets sh_addralign to 1, which makes libelf fail with
240
+ * misaligned section error during the update:
241
+ * FAILED elf_update(WRITE): invalid section alignment
242
+ *
243
+ * While waiting for ld fix, we fix the compressed sections
244
+ * sh_addralign value manualy.
245
+ */
246
+ static int compressed_section_fix (Elf * elf , Elf_Scn * scn , GElf_Shdr * sh )
247
+ {
248
+ int expected = gelf_getclass (elf ) == ELFCLASS32 ? 4 : 8 ;
249
+
250
+ if (!(sh -> sh_flags & SHF_COMPRESSED ))
251
+ return 0 ;
252
+
253
+ if (sh -> sh_addralign == expected )
254
+ return 0 ;
255
+
256
+ pr_debug2 (" - fixing wrong alignment sh_addralign %u, expected %u\n" ,
257
+ sh -> sh_addralign , expected );
258
+
259
+ sh -> sh_addralign = expected ;
260
+
261
+ if (gelf_update_shdr (scn , sh ) == 0 ) {
262
+ printf ("FAILED cannot update section header: %s\n" ,
263
+ elf_errmsg (-1 ));
264
+ return -1 ;
265
+ }
266
+ return 0 ;
267
+ }
268
+
236
269
static int elf_collect (struct object * obj )
237
270
{
238
271
Elf_Scn * scn = NULL ;
@@ -309,6 +342,9 @@ static int elf_collect(struct object *obj)
309
342
obj -> efile .idlist_shndx = idx ;
310
343
obj -> efile .idlist_addr = sh .sh_addr ;
311
344
}
345
+
346
+ if (compressed_section_fix (elf , scn , & sh ))
347
+ return -1 ;
312
348
}
313
349
314
350
return 0 ;
0 commit comments