Skip to content

Commit add4eed

Browse files
amlutoH. Peter Anvin
authored andcommitted
x86/vdso, build: Fix cross-compilation from big-endian architectures
This adds a macro GET(x) to convert x from big-endian to little-endian. Hopefully I put it everywhere it needs to go and got all the cases needed for everyone's linux/elf.h. Signed-off-by: Andy Lutomirski <luto@amacapital.net> Link: http://lkml.kernel.org/r/2cf258df123cb24bad63c274c8563c050547d99d.1401464755.git.luto@amacapital.net Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
1 parent 0115618 commit add4eed

2 files changed

Lines changed: 50 additions & 28 deletions

File tree

‎arch/x86/vdso/vdso2c.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ static void fail(const char *format, ...)
5151
va_end(ap);
5252
}
5353

54+
/*
55+
* Evil macros to do a little-endian read.
56+
*/
57+
#define __GET_TYPE(x, type, bits, ifnot) \
58+
__builtin_choose_expr( \
59+
__builtin_types_compatible_p(typeof(x), type), \
60+
le##bits##toh((x)), ifnot)
61+
62+
extern void bad_get(uint64_t);
63+
64+
#define GET(x) \
65+
__GET_TYPE((x), __u32, 32, __GET_TYPE((x), __u64, 64, \
66+
__GET_TYPE((x), __s32, 32, __GET_TYPE((x), __s64, 64, \
67+
__GET_TYPE((x), __u16, 16, bad_get(x))))))
68+
5469
#define NSYMS (sizeof(required_syms) / sizeof(required_syms[0]))
5570

5671
#define BITS 64

‎arch/x86/vdso/vdso2c.h‎

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,69 +18,76 @@ static void GOFUNC(void *addr, size_t len, FILE *outfile, const char *name)
1818
const char *secstrings;
1919
uint64_t syms[NSYMS] = {};
2020

21-
Elf_Phdr *pt = (Elf_Phdr *)(addr + hdr->e_phoff);
21+
Elf_Phdr *pt = (Elf_Phdr *)(addr + GET(hdr->e_phoff));
2222

2323
/* Walk the segment table. */
24-
for (i = 0; i < hdr->e_phnum; i++) {
25-
if (pt[i].p_type == PT_LOAD) {
24+
for (i = 0; i < GET(hdr->e_phnum); i++) {
25+
if (GET(pt[i].p_type) == PT_LOAD) {
2626
if (found_load)
2727
fail("multiple PT_LOAD segs\n");
2828

29-
if (pt[i].p_offset != 0 || pt[i].p_vaddr != 0)
29+
if (GET(pt[i].p_offset) != 0 ||
30+
GET(pt[i].p_vaddr) != 0)
3031
fail("PT_LOAD in wrong place\n");
3132

32-
if (pt[i].p_memsz != pt[i].p_filesz)
33+
if (GET(pt[i].p_memsz) != GET(pt[i].p_filesz))
3334
fail("cannot handle memsz != filesz\n");
3435

35-
load_size = pt[i].p_memsz;
36+
load_size = GET(pt[i].p_memsz);
3637
found_load = 1;
37-
} else if (pt[i].p_type == PT_DYNAMIC) {
38-
dyn = addr + pt[i].p_offset;
39-
dyn_end = addr + pt[i].p_offset + pt[i].p_memsz;
38+
} else if (GET(pt[i].p_type) == PT_DYNAMIC) {
39+
dyn = addr + GET(pt[i].p_offset);
40+
dyn_end = addr + GET(pt[i].p_offset) +
41+
GET(pt[i].p_memsz);
4042
}
4143
}
4244
if (!found_load)
4345
fail("no PT_LOAD seg\n");
4446
data_size = (load_size + 4095) / 4096 * 4096;
4547

4648
/* Walk the dynamic table */
47-
for (i = 0; dyn + i < dyn_end && dyn[i].d_tag != DT_NULL; i++) {
48-
if (dyn[i].d_tag == DT_REL || dyn[i].d_tag == DT_RELSZ ||
49-
dyn[i].d_tag == DT_RELENT || dyn[i].d_tag == DT_TEXTREL)
49+
for (i = 0; dyn + i < dyn_end && GET(dyn[i].d_tag) != DT_NULL; i++) {
50+
typeof(dyn[i].d_tag) tag = GET(dyn[i].d_tag);
51+
if (tag == DT_REL || tag == DT_RELSZ ||
52+
tag == DT_RELENT || tag == DT_TEXTREL)
5053
fail("vdso image contains dynamic relocations\n");
5154
}
5255

5356
/* Walk the section table */
54-
secstrings_hdr = addr + hdr->e_shoff + hdr->e_shentsize*hdr->e_shstrndx;
55-
secstrings = addr + secstrings_hdr->sh_offset;
56-
for (i = 0; i < hdr->e_shnum; i++) {
57-
Elf_Shdr *sh = addr + hdr->e_shoff + hdr->e_shentsize * i;
58-
if (sh->sh_type == SHT_SYMTAB)
57+
secstrings_hdr = addr + GET(hdr->e_shoff) +
58+
GET(hdr->e_shentsize)*GET(hdr->e_shstrndx);
59+
secstrings = addr + GET(secstrings_hdr->sh_offset);
60+
for (i = 0; i < GET(hdr->e_shnum); i++) {
61+
Elf_Shdr *sh = addr + GET(hdr->e_shoff) +
62+
GET(hdr->e_shentsize) * i;
63+
if (GET(sh->sh_type) == SHT_SYMTAB)
5964
symtab_hdr = sh;
6065

61-
if (!strcmp(secstrings + sh->sh_name, ".altinstructions"))
66+
if (!strcmp(secstrings + GET(sh->sh_name), ".altinstructions"))
6267
alt_sec = sh;
6368
}
6469

6570
if (!symtab_hdr)
6671
fail("no symbol table\n");
6772

68-
strtab_hdr = addr + hdr->e_shoff +
69-
hdr->e_shentsize * symtab_hdr->sh_link;
73+
strtab_hdr = addr + GET(hdr->e_shoff) +
74+
GET(hdr->e_shentsize) * GET(symtab_hdr->sh_link);
7075

7176
/* Walk the symbol table */
72-
for (i = 0; i < symtab_hdr->sh_size / symtab_hdr->sh_entsize; i++) {
77+
for (i = 0; i < GET(symtab_hdr->sh_size) / GET(symtab_hdr->sh_entsize);
78+
i++) {
7379
int k;
74-
Elf_Sym *sym = addr + symtab_hdr->sh_offset +
75-
symtab_hdr->sh_entsize * i;
76-
const char *name = addr + strtab_hdr->sh_offset + sym->st_name;
80+
Elf_Sym *sym = addr + GET(symtab_hdr->sh_offset) +
81+
GET(symtab_hdr->sh_entsize) * i;
82+
const char *name = addr + GET(strtab_hdr->sh_offset) +
83+
GET(sym->st_name);
7784
for (k = 0; k < NSYMS; k++) {
7885
if (!strcmp(name, required_syms[k])) {
7986
if (syms[k]) {
8087
fail("duplicate symbol %s\n",
8188
required_syms[k]);
8289
}
83-
syms[k] = sym->st_value;
90+
syms[k] = GET(sym->st_value);
8491
}
8592
}
8693
}
@@ -106,7 +113,7 @@ static void GOFUNC(void *addr, size_t len, FILE *outfile, const char *name)
106113
hdr->e_shoff = 0;
107114
hdr->e_shentsize = 0;
108115
hdr->e_shnum = 0;
109-
hdr->e_shstrndx = SHN_UNDEF;
116+
hdr->e_shstrndx = htole16(SHN_UNDEF);
110117

111118
if (!name) {
112119
fwrite(addr, load_size, 1, outfile);
@@ -140,9 +147,9 @@ static void GOFUNC(void *addr, size_t len, FILE *outfile, const char *name)
140147
fprintf(outfile, "\t},\n");
141148
if (alt_sec) {
142149
fprintf(outfile, "\t.alt = %lu,\n",
143-
(unsigned long)alt_sec->sh_offset);
150+
(unsigned long)GET(alt_sec->sh_offset));
144151
fprintf(outfile, "\t.alt_len = %lu,\n",
145-
(unsigned long)alt_sec->sh_size);
152+
(unsigned long)GET(alt_sec->sh_size));
146153
}
147154
for (i = 0; i < NSYMS; i++) {
148155
if (syms[i])

0 commit comments

Comments
 (0)