Skip to content

Commit c191920

Browse files
author
H. Peter Anvin
committed
x86/vdso, build: Make LE access macros clearer, host-safe
Make it a little clearer what the littleendian access macros in vdso2c.[ch] actually do. This way they can probably also be moved to a central location (e.g. tools/include) for the benefit of other host tools. We should avoid implementation namespace symbols when writing code that is compiling for the compiler host, so avoid names starting with double underscore or underscore-capital. Signed-off-by: H. Peter Anvin <hpa@linux.intel.com> Cc: Andy Lutomirski <luto@amacapital.net> Link: http://lkml.kernel.org/r/2cf258df123cb24bad63c274c8563c050547d99d.1401464755.git.luto@amacapital.net
1 parent add4eed commit c191920

2 files changed

Lines changed: 42 additions & 39 deletions

File tree

‎arch/x86/vdso/vdso2c.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ static void fail(const char *format, ...)
5454
/*
5555
* Evil macros to do a little-endian read.
5656
*/
57-
#define __GET_TYPE(x, type, bits, ifnot) \
57+
#define GLE(x, bits, ifnot) \
5858
__builtin_choose_expr( \
59-
__builtin_types_compatible_p(typeof(x), type), \
60-
le##bits##toh((x)), ifnot)
59+
(sizeof(x) == bits/8), \
60+
(__typeof__(x))le##bits##toh(x), ifnot)
6161

62-
extern void bad_get(uint64_t);
62+
extern void bad_get_le(uint64_t);
63+
#define LAST_LE(x) \
64+
__builtin_choose_expr(sizeof(x) == 1, (x), bad_get_le(x))
6365

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))))))
66+
#define GET_LE(x) \
67+
GLE(x, 64, GLE(x, 32, GLE(x, 16, LAST_LE(x))))
6868

6969
#define NSYMS (sizeof(required_syms) / sizeof(required_syms[0]))
7070

‎arch/x86/vdso/vdso2c.h‎

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,76 +18,79 @@ 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 + GET(hdr->e_phoff));
21+
Elf_Phdr *pt = (Elf_Phdr *)(addr + GET_LE(hdr->e_phoff));
2222

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

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

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

36-
load_size = GET(pt[i].p_memsz);
36+
load_size = GET_LE(pt[i].p_memsz);
3737
found_load = 1;
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);
38+
} else if (GET_LE(pt[i].p_type) == PT_DYNAMIC) {
39+
dyn = addr + GET_LE(pt[i].p_offset);
40+
dyn_end = addr + GET_LE(pt[i].p_offset) +
41+
GET_LE(pt[i].p_memsz);
4242
}
4343
}
4444
if (!found_load)
4545
fail("no PT_LOAD seg\n");
4646
data_size = (load_size + 4095) / 4096 * 4096;
4747

4848
/* Walk the dynamic table */
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);
49+
for (i = 0; dyn + i < dyn_end &&
50+
GET_LE(dyn[i].d_tag) != DT_NULL; i++) {
51+
typeof(dyn[i].d_tag) tag = GET_LE(dyn[i].d_tag);
5152
if (tag == DT_REL || tag == DT_RELSZ ||
5253
tag == DT_RELENT || tag == DT_TEXTREL)
5354
fail("vdso image contains dynamic relocations\n");
5455
}
5556

5657
/* Walk the section table */
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)
58+
secstrings_hdr = addr + GET_LE(hdr->e_shoff) +
59+
GET_LE(hdr->e_shentsize)*GET_LE(hdr->e_shstrndx);
60+
secstrings = addr + GET_LE(secstrings_hdr->sh_offset);
61+
for (i = 0; i < GET_LE(hdr->e_shnum); i++) {
62+
Elf_Shdr *sh = addr + GET_LE(hdr->e_shoff) +
63+
GET_LE(hdr->e_shentsize) * i;
64+
if (GET_LE(sh->sh_type) == SHT_SYMTAB)
6465
symtab_hdr = sh;
6566

66-
if (!strcmp(secstrings + GET(sh->sh_name), ".altinstructions"))
67+
if (!strcmp(secstrings + GET_LE(sh->sh_name),
68+
".altinstructions"))
6769
alt_sec = sh;
6870
}
6971

7072
if (!symtab_hdr)
7173
fail("no symbol table\n");
7274

73-
strtab_hdr = addr + GET(hdr->e_shoff) +
74-
GET(hdr->e_shentsize) * GET(symtab_hdr->sh_link);
75+
strtab_hdr = addr + GET_LE(hdr->e_shoff) +
76+
GET_LE(hdr->e_shentsize) * GET_LE(symtab_hdr->sh_link);
7577

7678
/* Walk the symbol table */
77-
for (i = 0; i < GET(symtab_hdr->sh_size) / GET(symtab_hdr->sh_entsize);
79+
for (i = 0;
80+
i < GET_LE(symtab_hdr->sh_size) / GET_LE(symtab_hdr->sh_entsize);
7881
i++) {
7982
int k;
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);
83+
Elf_Sym *sym = addr + GET_LE(symtab_hdr->sh_offset) +
84+
GET_LE(symtab_hdr->sh_entsize) * i;
85+
const char *name = addr + GET_LE(strtab_hdr->sh_offset) +
86+
GET_LE(sym->st_name);
8487
for (k = 0; k < NSYMS; k++) {
8588
if (!strcmp(name, required_syms[k])) {
8689
if (syms[k]) {
8790
fail("duplicate symbol %s\n",
8891
required_syms[k]);
8992
}
90-
syms[k] = GET(sym->st_value);
93+
syms[k] = GET_LE(sym->st_value);
9194
}
9295
}
9396
}
@@ -147,9 +150,9 @@ static void GOFUNC(void *addr, size_t len, FILE *outfile, const char *name)
147150
fprintf(outfile, "\t},\n");
148151
if (alt_sec) {
149152
fprintf(outfile, "\t.alt = %lu,\n",
150-
(unsigned long)GET(alt_sec->sh_offset));
153+
(unsigned long)GET_LE(alt_sec->sh_offset));
151154
fprintf(outfile, "\t.alt_len = %lu,\n",
152-
(unsigned long)GET(alt_sec->sh_size));
155+
(unsigned long)GET_LE(alt_sec->sh_size));
153156
}
154157
for (i = 0; i < NSYMS; i++) {
155158
if (syms[i])

0 commit comments

Comments
 (0)