1 // SPDX-License-Identifier: GPL-2.0
18 #define ELF_MACHINE EM_S390
19 #define ELF_MACHINE_NAME "IBM S/390"
20 #define SHT_REL_TYPE SHT_RELA
21 #define Elf_Rel Elf64_Rela
23 #define ELF_CLASS ELFCLASS64
24 #define ELF_ENDIAN ELFDATA2MSB
25 #define ELF_R_SYM(val) ELF64_R_SYM(val)
26 #define ELF_R_TYPE(val) ELF64_R_TYPE(val)
27 #define ELF_ST_TYPE(o) ELF64_ST_TYPE(o)
28 #define ELF_ST_BIND(o) ELF64_ST_BIND(o)
29 #define ELF_ST_VISIBILITY(o) ELF64_ST_VISIBILITY(o)
31 #define ElfW(type) _ElfW(ELF_BITS, type)
32 #define _ElfW(bits, type) __ElfW(bits, type)
33 #define __ElfW(bits, type) Elf##bits##_##type
35 #define Elf_Addr ElfW(Addr)
36 #define Elf_Ehdr ElfW(Ehdr)
37 #define Elf_Phdr ElfW(Phdr)
38 #define Elf_Shdr ElfW(Shdr)
39 #define Elf_Sym ElfW(Sym)
42 static unsigned long shnum;
43 static unsigned int shstrndx;
51 static struct relocs relocs64;
60 static struct section *secs;
62 #if BYTE_ORDER == LITTLE_ENDIAN
63 #define le16_to_cpu(val) (val)
64 #define le32_to_cpu(val) (val)
65 #define le64_to_cpu(val) (val)
66 #define be16_to_cpu(val) bswap_16(val)
67 #define be32_to_cpu(val) bswap_32(val)
68 #define be64_to_cpu(val) bswap_64(val)
71 #if BYTE_ORDER == BIG_ENDIAN
72 #define le16_to_cpu(val) bswap_16(val)
73 #define le32_to_cpu(val) bswap_32(val)
74 #define le64_to_cpu(val) bswap_64(val)
75 #define be16_to_cpu(val) (val)
76 #define be32_to_cpu(val) (val)
77 #define be64_to_cpu(val) (val)
80 static uint16_t elf16_to_cpu(uint16_t val)
82 if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
83 return le16_to_cpu(val);
85 return be16_to_cpu(val);
88 static uint32_t elf32_to_cpu(uint32_t val)
90 if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
91 return le32_to_cpu(val);
93 return be32_to_cpu(val);
96 #define elf_half_to_cpu(x) elf16_to_cpu(x)
97 #define elf_word_to_cpu(x) elf32_to_cpu(x)
99 static uint64_t elf64_to_cpu(uint64_t val)
101 return be64_to_cpu(val);
104 #define elf_addr_to_cpu(x) elf64_to_cpu(x)
105 #define elf_off_to_cpu(x) elf64_to_cpu(x)
106 #define elf_xword_to_cpu(x) elf64_to_cpu(x)
108 static void die(char *fmt, ...)
113 vfprintf(stderr, fmt, ap);
118 static void read_ehdr(FILE *fp)
120 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
121 die("Cannot read ELF header: %s\n", strerror(errno));
122 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0)
123 die("No ELF magic\n");
124 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
125 die("Not a %d bit executable\n", ELF_BITS);
126 if (ehdr.e_ident[EI_DATA] != ELF_ENDIAN)
127 die("ELF endian mismatch\n");
128 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
129 die("Unknown ELF version\n");
131 /* Convert the fields to native endian */
132 ehdr.e_type = elf_half_to_cpu(ehdr.e_type);
133 ehdr.e_machine = elf_half_to_cpu(ehdr.e_machine);
134 ehdr.e_version = elf_word_to_cpu(ehdr.e_version);
135 ehdr.e_entry = elf_addr_to_cpu(ehdr.e_entry);
136 ehdr.e_phoff = elf_off_to_cpu(ehdr.e_phoff);
137 ehdr.e_shoff = elf_off_to_cpu(ehdr.e_shoff);
138 ehdr.e_flags = elf_word_to_cpu(ehdr.e_flags);
139 ehdr.e_ehsize = elf_half_to_cpu(ehdr.e_ehsize);
140 ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
141 ehdr.e_phnum = elf_half_to_cpu(ehdr.e_phnum);
142 ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
143 ehdr.e_shnum = elf_half_to_cpu(ehdr.e_shnum);
144 ehdr.e_shstrndx = elf_half_to_cpu(ehdr.e_shstrndx);
146 shnum = ehdr.e_shnum;
147 shstrndx = ehdr.e_shstrndx;
149 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN))
150 die("Unsupported ELF header type\n");
151 if (ehdr.e_machine != ELF_MACHINE)
152 die("Not for %s\n", ELF_MACHINE_NAME);
153 if (ehdr.e_version != EV_CURRENT)
154 die("Unknown ELF version\n");
155 if (ehdr.e_ehsize != sizeof(Elf_Ehdr))
156 die("Bad Elf header size\n");
157 if (ehdr.e_phentsize != sizeof(Elf_Phdr))
158 die("Bad program header entry\n");
159 if (ehdr.e_shentsize != sizeof(Elf_Shdr))
160 die("Bad section header entry\n");
162 if (shnum == SHN_UNDEF || shstrndx == SHN_XINDEX) {
165 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
166 die("Seek to %" FMT " failed: %s\n", ehdr.e_shoff, strerror(errno));
168 if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
169 die("Cannot read initial ELF section header: %s\n", strerror(errno));
171 if (shnum == SHN_UNDEF)
172 shnum = elf_xword_to_cpu(shdr.sh_size);
174 if (shstrndx == SHN_XINDEX)
175 shstrndx = elf_word_to_cpu(shdr.sh_link);
178 if (shstrndx >= shnum)
179 die("String table index out of bounds\n");
182 static void read_shdrs(FILE *fp)
187 secs = calloc(shnum, sizeof(struct section));
189 die("Unable to allocate %ld section headers\n", shnum);
191 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
192 die("Seek to %" FMT " failed: %s\n", ehdr.e_shoff, strerror(errno));
194 for (i = 0; i < shnum; i++) {
195 struct section *sec = &secs[i];
197 if (fread(&shdr, sizeof(shdr), 1, fp) != 1) {
198 die("Cannot read ELF section headers %d/%ld: %s\n",
199 i, shnum, strerror(errno));
202 sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
203 sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
204 sec->shdr.sh_flags = elf_xword_to_cpu(shdr.sh_flags);
205 sec->shdr.sh_addr = elf_addr_to_cpu(shdr.sh_addr);
206 sec->shdr.sh_offset = elf_off_to_cpu(shdr.sh_offset);
207 sec->shdr.sh_size = elf_xword_to_cpu(shdr.sh_size);
208 sec->shdr.sh_link = elf_word_to_cpu(shdr.sh_link);
209 sec->shdr.sh_info = elf_word_to_cpu(shdr.sh_info);
210 sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
211 sec->shdr.sh_entsize = elf_xword_to_cpu(shdr.sh_entsize);
213 if (sec->shdr.sh_link < shnum)
214 sec->link = &secs[sec->shdr.sh_link];
219 static void read_relocs(FILE *fp)
223 for (i = 0; i < shnum; i++) {
224 struct section *sec = &secs[i];
226 if (sec->shdr.sh_type != SHT_REL_TYPE)
229 sec->reltab = malloc(sec->shdr.sh_size);
231 die("malloc of %" FMT " bytes for relocs failed\n", sec->shdr.sh_size);
233 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0)
234 die("Seek to %" FMT " failed: %s\n", sec->shdr.sh_offset, strerror(errno));
236 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp) != sec->shdr.sh_size)
237 die("Cannot read symbol table: %s\n", strerror(errno));
239 for (j = 0; j < sec->shdr.sh_size / sizeof(Elf_Rel); j++) {
240 Elf_Rel *rel = &sec->reltab[j];
242 rel->r_offset = elf_addr_to_cpu(rel->r_offset);
243 rel->r_info = elf_xword_to_cpu(rel->r_info);
244 #if (SHT_REL_TYPE == SHT_RELA)
245 rel->r_addend = elf_xword_to_cpu(rel->r_addend);
251 static void add_reloc(struct relocs *r, uint32_t offset)
253 if (r->count == r->size) {
254 unsigned long newsize = r->size + 50000;
255 void *mem = realloc(r->offset, newsize * sizeof(r->offset[0]));
258 die("realloc of %ld entries for relocs failed\n", newsize);
263 r->offset[r->count++] = offset;
266 static int do_reloc(struct section *sec, Elf_Rel *rel)
268 unsigned int r_type = ELF64_R_TYPE(rel->r_info);
269 ElfW(Addr) offset = rel->r_offset;
283 add_reloc(&relocs64, offset);
286 die("Unsupported relocation type: %d\n", r_type);
293 static void walk_relocs(void)
297 /* Walk through the relocations */
298 for (i = 0; i < shnum; i++) {
299 struct section *sec_applies;
301 struct section *sec = &secs[i];
303 if (sec->shdr.sh_type != SHT_REL_TYPE)
306 sec_applies = &secs[sec->shdr.sh_info];
307 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC))
310 for (j = 0; j < sec->shdr.sh_size / sizeof(Elf_Rel); j++) {
311 Elf_Rel *rel = &sec->reltab[j];
318 static int cmp_relocs(const void *va, const void *vb)
320 const uint32_t *a, *b;
323 return (*a == *b) ? 0 : (*a > *b) ? 1 : -1;
326 static void sort_relocs(struct relocs *r)
328 qsort(r->offset, r->count, sizeof(r->offset[0]), cmp_relocs);
331 static int print_reloc(uint32_t v)
333 return fprintf(stdout, "\t.long 0x%08"PRIx32"\n", v) > 0 ? 0 : -1;
336 static void emit_relocs(void)
341 sort_relocs(&relocs64);
343 printf(".section \".vmlinux.relocs_64\",\"a\"\n");
344 for (i = 0; i < relocs64.count; i++)
345 print_reloc(relocs64.offset[i]);
348 static void process(FILE *fp)
356 static void usage(void)
358 die("relocs vmlinux\n");
361 int main(int argc, char **argv)
363 unsigned char e_ident[EI_NIDENT];
374 fp = fopen(fname, "r");
376 die("Cannot open %s: %s\n", fname, strerror(errno));
378 if (fread(&e_ident, 1, EI_NIDENT, fp) != EI_NIDENT)
379 die("Cannot read %s: %s", fname, strerror(errno));