Skip to content

Commit 8fe1a63

Browse files
committed
modpost: work around unaligned data access error
With the latest binutils, modpost fails with a bus error on some architectures such as ARM and sparc64. Since binutils commit 1f1b5e506bf0 ("bfd/ELF: restrict file alignment for object files"), the byte offset to each section (sh_offset) in relocatable ELF is no longer guaranteed to be aligned. modpost parses MODULE_DEVICE_TABLE() data structures, which are usually located in the .rodata section. If it is not properly aligned, unaligned access errors may occur. To address the issue, this commit imports the get_unaligned() helper from include/linux/unaligned.h. The get_unaligned_native() helper caters to the endianness in addition to handling the unaligned access. I slightly refactored do_pcmcia_entry() and do_input() to avoid writing back to an unaligned address. (We would need the put_unaligned() helper to do that.) The addend_*_rel() functions need similar adjustments because the .text sections are not aligned either. It seems that the .symtab, .rel.* and .rela.* sections are still aligned. Keep normal pointer access for these sections to avoid unnecessary performance costs. Reported-by: Paulo Pisati <[email protected]> Reported-by: Matthias Klose <[email protected]> Closes: https://sourceware.org/bugzilla/show_bug.cgi?id=32435 Reported-by: John Paul Adrian Glaubitz <[email protected]> Closes: https://sourceware.org/bugzilla/show_bug.cgi?id=32493 Signed-off-by: Masahiro Yamada <[email protected]> Tested-by: John Paul Adrian Glaubitz <[email protected]>
1 parent e1352d7 commit 8fe1a63

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

scripts/mod/file2alias.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ struct devtable {
132132
* based at address m.
133133
*/
134134
#define DEF_FIELD(m, devid, f) \
135-
typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
135+
typeof(((struct devid *)0)->f) f = \
136+
get_unaligned_native((typeof(f) *)((m) + OFF_##devid##_##f))
136137

137138
/* Define a variable f that holds the address of field f of struct devid
138139
* based at address m. Due to the way typeof works, for a field of type
@@ -600,7 +601,7 @@ static void do_pnp_card_entry(struct module *mod, void *symval)
600601
static void do_pcmcia_entry(struct module *mod, void *symval)
601602
{
602603
char alias[256] = {};
603-
unsigned int i;
604+
604605
DEF_FIELD(symval, pcmcia_device_id, match_flags);
605606
DEF_FIELD(symval, pcmcia_device_id, manf_id);
606607
DEF_FIELD(symval, pcmcia_device_id, card_id);
@@ -609,10 +610,6 @@ static void do_pcmcia_entry(struct module *mod, void *symval)
609610
DEF_FIELD(symval, pcmcia_device_id, device_no);
610611
DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
611612

612-
for (i=0; i<4; i++) {
613-
(*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]);
614-
}
615-
616613
ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
617614
manf_id);
618615
ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
@@ -623,10 +620,14 @@ static void do_pcmcia_entry(struct module *mod, void *symval)
623620
function);
624621
ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
625622
device_no);
626-
ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]);
627-
ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]);
628-
ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]);
629-
ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]);
623+
ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1,
624+
get_unaligned_native(*prod_id_hash + 0));
625+
ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2,
626+
get_unaligned_native(*prod_id_hash + 1));
627+
ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3,
628+
get_unaligned_native(*prod_id_hash + 2));
629+
ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4,
630+
get_unaligned_native(*prod_id_hash + 3));
630631

631632
module_alias_printf(mod, true, "pcmcia:%s", alias);
632633
}
@@ -654,10 +655,9 @@ static void do_input(char *alias,
654655
{
655656
unsigned int i;
656657

657-
for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++)
658-
arr[i] = TO_NATIVE(arr[i]);
659658
for (i = min; i <= max; i++)
660-
if (arr[i / BITS_PER_LONG] & (1ULL << (i%BITS_PER_LONG)))
659+
if (get_unaligned_native(arr + i / BITS_PER_LONG) &
660+
(1ULL << (i % BITS_PER_LONG)))
661661
sprintf(alias + strlen(alias), "%X,*", i);
662662
}
663663

scripts/mod/modpost.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,9 +1138,9 @@ static Elf_Addr addend_386_rel(uint32_t *location, unsigned int r_type)
11381138
{
11391139
switch (r_type) {
11401140
case R_386_32:
1141-
return TO_NATIVE(*location);
1141+
return get_unaligned_native(location);
11421142
case R_386_PC32:
1143-
return TO_NATIVE(*location) + 4;
1143+
return get_unaligned_native(location) + 4;
11441144
}
11451145

11461146
return (Elf_Addr)(-1);
@@ -1161,24 +1161,24 @@ static Elf_Addr addend_arm_rel(void *loc, Elf_Sym *sym, unsigned int r_type)
11611161
switch (r_type) {
11621162
case R_ARM_ABS32:
11631163
case R_ARM_REL32:
1164-
inst = TO_NATIVE(*(uint32_t *)loc);
1164+
inst = get_unaligned_native((uint32_t *)loc);
11651165
return inst + sym->st_value;
11661166
case R_ARM_MOVW_ABS_NC:
11671167
case R_ARM_MOVT_ABS:
1168-
inst = TO_NATIVE(*(uint32_t *)loc);
1168+
inst = get_unaligned_native((uint32_t *)loc);
11691169
offset = sign_extend32(((inst & 0xf0000) >> 4) | (inst & 0xfff),
11701170
15);
11711171
return offset + sym->st_value;
11721172
case R_ARM_PC24:
11731173
case R_ARM_CALL:
11741174
case R_ARM_JUMP24:
1175-
inst = TO_NATIVE(*(uint32_t *)loc);
1175+
inst = get_unaligned_native((uint32_t *)loc);
11761176
offset = sign_extend32((inst & 0x00ffffff) << 2, 25);
11771177
return offset + sym->st_value + 8;
11781178
case R_ARM_THM_MOVW_ABS_NC:
11791179
case R_ARM_THM_MOVT_ABS:
1180-
upper = TO_NATIVE(*(uint16_t *)loc);
1181-
lower = TO_NATIVE(*((uint16_t *)loc + 1));
1180+
upper = get_unaligned_native((uint16_t *)loc);
1181+
lower = get_unaligned_native((uint16_t *)loc + 1);
11821182
offset = sign_extend32(((upper & 0x000f) << 12) |
11831183
((upper & 0x0400) << 1) |
11841184
((lower & 0x7000) >> 4) |
@@ -1195,8 +1195,8 @@ static Elf_Addr addend_arm_rel(void *loc, Elf_Sym *sym, unsigned int r_type)
11951195
* imm11 = lower[10:0]
11961196
* imm32 = SignExtend(S:J2:J1:imm6:imm11:'0')
11971197
*/
1198-
upper = TO_NATIVE(*(uint16_t *)loc);
1199-
lower = TO_NATIVE(*((uint16_t *)loc + 1));
1198+
upper = get_unaligned_native((uint16_t *)loc);
1199+
lower = get_unaligned_native((uint16_t *)loc + 1);
12001200

12011201
sign = (upper >> 10) & 1;
12021202
j1 = (lower >> 13) & 1;
@@ -1219,8 +1219,8 @@ static Elf_Addr addend_arm_rel(void *loc, Elf_Sym *sym, unsigned int r_type)
12191219
* I2 = NOT(J2 XOR S)
12201220
* imm32 = SignExtend(S:I1:I2:imm10:imm11:'0')
12211221
*/
1222-
upper = TO_NATIVE(*(uint16_t *)loc);
1223-
lower = TO_NATIVE(*((uint16_t *)loc + 1));
1222+
upper = get_unaligned_native((uint16_t *)loc);
1223+
lower = get_unaligned_native((uint16_t *)loc + 1);
12241224

12251225
sign = (upper >> 10) & 1;
12261226
j1 = (lower >> 13) & 1;
@@ -1241,7 +1241,7 @@ static Elf_Addr addend_mips_rel(uint32_t *location, unsigned int r_type)
12411241
{
12421242
uint32_t inst;
12431243

1244-
inst = TO_NATIVE(*location);
1244+
inst = get_unaligned_native(location);
12451245
switch (r_type) {
12461246
case R_MIPS_LO16:
12471247
return inst & 0xffff;

scripts/mod/modpost.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@
6565
#define TO_NATIVE(x) \
6666
(target_is_big_endian == host_is_big_endian ? x : bswap(x))
6767

68+
#define __get_unaligned_t(type, ptr) ({ \
69+
const struct { type x; } __attribute__((__packed__)) *__pptr = \
70+
(typeof(__pptr))(ptr); \
71+
__pptr->x; \
72+
})
73+
74+
#define get_unaligned(ptr) __get_unaligned_t(typeof(*(ptr)), (ptr))
75+
76+
#define get_unaligned_native(ptr) \
77+
({ \
78+
typeof(*(ptr)) _val = get_unaligned(ptr); \
79+
TO_NATIVE(_val); \
80+
})
81+
6882
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
6983

7084
#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)

0 commit comments

Comments
 (0)