Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* This is included from relocs_32/64.c */
0003 
0004 #define ElfW(type)      _ElfW(ELF_BITS, type)
0005 #define _ElfW(bits, type)   __ElfW(bits, type)
0006 #define __ElfW(bits, type)  Elf##bits##_##type
0007 
0008 #define Elf_Addr        ElfW(Addr)
0009 #define Elf_Ehdr        ElfW(Ehdr)
0010 #define Elf_Phdr        ElfW(Phdr)
0011 #define Elf_Shdr        ElfW(Shdr)
0012 #define Elf_Sym         ElfW(Sym)
0013 
0014 static Elf_Ehdr ehdr;
0015 
0016 struct relocs {
0017     uint32_t    *offset;
0018     unsigned long   count;
0019     unsigned long   size;
0020 };
0021 
0022 static struct relocs relocs;
0023 
0024 struct section {
0025     Elf_Shdr       shdr;
0026     struct section *link;
0027     Elf_Sym        *symtab;
0028     Elf_Rel        *reltab;
0029     char           *strtab;
0030     long           shdr_offset;
0031 };
0032 static struct section *secs;
0033 
0034 static const char * const regex_sym_kernel = {
0035 /* Symbols matching these regex's should never be relocated */
0036     "^(__crc_)",
0037 };
0038 
0039 static regex_t sym_regex_c;
0040 
0041 static int regex_skip_reloc(const char *sym_name)
0042 {
0043     return !regexec(&sym_regex_c, sym_name, 0, NULL, 0);
0044 }
0045 
0046 static void regex_init(void)
0047 {
0048     char errbuf[128];
0049     int err;
0050 
0051     err = regcomp(&sym_regex_c, regex_sym_kernel,
0052             REG_EXTENDED|REG_NOSUB);
0053 
0054     if (err) {
0055         regerror(err, &sym_regex_c, errbuf, sizeof(errbuf));
0056         die("%s", errbuf);
0057     }
0058 }
0059 
0060 static const char *rel_type(unsigned type)
0061 {
0062     static const char * const type_name[] = {
0063 #define REL_TYPE(X)[X] = #X
0064         REL_TYPE(R_MIPS_NONE),
0065         REL_TYPE(R_MIPS_16),
0066         REL_TYPE(R_MIPS_32),
0067         REL_TYPE(R_MIPS_REL32),
0068         REL_TYPE(R_MIPS_26),
0069         REL_TYPE(R_MIPS_HI16),
0070         REL_TYPE(R_MIPS_LO16),
0071         REL_TYPE(R_MIPS_GPREL16),
0072         REL_TYPE(R_MIPS_LITERAL),
0073         REL_TYPE(R_MIPS_GOT16),
0074         REL_TYPE(R_MIPS_PC16),
0075         REL_TYPE(R_MIPS_CALL16),
0076         REL_TYPE(R_MIPS_GPREL32),
0077         REL_TYPE(R_MIPS_64),
0078         REL_TYPE(R_MIPS_HIGHER),
0079         REL_TYPE(R_MIPS_HIGHEST),
0080         REL_TYPE(R_MIPS_PC21_S2),
0081         REL_TYPE(R_MIPS_PC26_S2),
0082 #undef REL_TYPE
0083     };
0084     const char *name = "unknown type rel type name";
0085 
0086     if (type < ARRAY_SIZE(type_name) && type_name[type])
0087         name = type_name[type];
0088     return name;
0089 }
0090 
0091 static const char *sec_name(unsigned shndx)
0092 {
0093     const char *sec_strtab;
0094     const char *name;
0095 
0096     sec_strtab = secs[ehdr.e_shstrndx].strtab;
0097     if (shndx < ehdr.e_shnum)
0098         name = sec_strtab + secs[shndx].shdr.sh_name;
0099     else if (shndx == SHN_ABS)
0100         name = "ABSOLUTE";
0101     else if (shndx == SHN_COMMON)
0102         name = "COMMON";
0103     else
0104         name = "<noname>";
0105     return name;
0106 }
0107 
0108 static struct section *sec_lookup(const char *secname)
0109 {
0110     int i;
0111 
0112     for (i = 0; i < ehdr.e_shnum; i++)
0113         if (strcmp(secname, sec_name(i)) == 0)
0114             return &secs[i];
0115 
0116     return NULL;
0117 }
0118 
0119 static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
0120 {
0121     const char *name;
0122 
0123     if (sym->st_name)
0124         name = sym_strtab + sym->st_name;
0125     else
0126         name = sec_name(sym->st_shndx);
0127     return name;
0128 }
0129 
0130 #if BYTE_ORDER == LITTLE_ENDIAN
0131 #define le16_to_cpu(val) (val)
0132 #define le32_to_cpu(val) (val)
0133 #define le64_to_cpu(val) (val)
0134 #define be16_to_cpu(val) bswap_16(val)
0135 #define be32_to_cpu(val) bswap_32(val)
0136 #define be64_to_cpu(val) bswap_64(val)
0137 
0138 #define cpu_to_le16(val) (val)
0139 #define cpu_to_le32(val) (val)
0140 #define cpu_to_le64(val) (val)
0141 #define cpu_to_be16(val) bswap_16(val)
0142 #define cpu_to_be32(val) bswap_32(val)
0143 #define cpu_to_be64(val) bswap_64(val)
0144 #endif
0145 #if BYTE_ORDER == BIG_ENDIAN
0146 #define le16_to_cpu(val) bswap_16(val)
0147 #define le32_to_cpu(val) bswap_32(val)
0148 #define le64_to_cpu(val) bswap_64(val)
0149 #define be16_to_cpu(val) (val)
0150 #define be32_to_cpu(val) (val)
0151 #define be64_to_cpu(val) (val)
0152 
0153 #define cpu_to_le16(val) bswap_16(val)
0154 #define cpu_to_le32(val) bswap_32(val)
0155 #define cpu_to_le64(val) bswap_64(val)
0156 #define cpu_to_be16(val) (val)
0157 #define cpu_to_be32(val) (val)
0158 #define cpu_to_be64(val) (val)
0159 #endif
0160 
0161 static uint16_t elf16_to_cpu(uint16_t val)
0162 {
0163     if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
0164         return le16_to_cpu(val);
0165     else
0166         return be16_to_cpu(val);
0167 }
0168 
0169 static uint32_t elf32_to_cpu(uint32_t val)
0170 {
0171     if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
0172         return le32_to_cpu(val);
0173     else
0174         return be32_to_cpu(val);
0175 }
0176 
0177 static uint32_t cpu_to_elf32(uint32_t val)
0178 {
0179     if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
0180         return cpu_to_le32(val);
0181     else
0182         return cpu_to_be32(val);
0183 }
0184 
0185 #define elf_half_to_cpu(x)  elf16_to_cpu(x)
0186 #define elf_word_to_cpu(x)  elf32_to_cpu(x)
0187 
0188 #if ELF_BITS == 64
0189 static uint64_t elf64_to_cpu(uint64_t val)
0190 {
0191     if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
0192         return le64_to_cpu(val);
0193     else
0194         return be64_to_cpu(val);
0195 }
0196 #define elf_addr_to_cpu(x)  elf64_to_cpu(x)
0197 #define elf_off_to_cpu(x)   elf64_to_cpu(x)
0198 #define elf_xword_to_cpu(x) elf64_to_cpu(x)
0199 #else
0200 #define elf_addr_to_cpu(x)  elf32_to_cpu(x)
0201 #define elf_off_to_cpu(x)   elf32_to_cpu(x)
0202 #define elf_xword_to_cpu(x) elf32_to_cpu(x)
0203 #endif
0204 
0205 static void read_ehdr(FILE *fp)
0206 {
0207     if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
0208         die("Cannot read ELF header: %s\n", strerror(errno));
0209 
0210     if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0)
0211         die("No ELF magic\n");
0212 
0213     if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
0214         die("Not a %d bit executable\n", ELF_BITS);
0215 
0216     if ((ehdr.e_ident[EI_DATA] != ELFDATA2LSB) &&
0217         (ehdr.e_ident[EI_DATA] != ELFDATA2MSB))
0218         die("Unknown ELF Endianness\n");
0219 
0220     if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
0221         die("Unknown ELF version\n");
0222 
0223     /* Convert the fields to native endian */
0224     ehdr.e_type      = elf_half_to_cpu(ehdr.e_type);
0225     ehdr.e_machine   = elf_half_to_cpu(ehdr.e_machine);
0226     ehdr.e_version   = elf_word_to_cpu(ehdr.e_version);
0227     ehdr.e_entry     = elf_addr_to_cpu(ehdr.e_entry);
0228     ehdr.e_phoff     = elf_off_to_cpu(ehdr.e_phoff);
0229     ehdr.e_shoff     = elf_off_to_cpu(ehdr.e_shoff);
0230     ehdr.e_flags     = elf_word_to_cpu(ehdr.e_flags);
0231     ehdr.e_ehsize    = elf_half_to_cpu(ehdr.e_ehsize);
0232     ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
0233     ehdr.e_phnum     = elf_half_to_cpu(ehdr.e_phnum);
0234     ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
0235     ehdr.e_shnum     = elf_half_to_cpu(ehdr.e_shnum);
0236     ehdr.e_shstrndx  = elf_half_to_cpu(ehdr.e_shstrndx);
0237 
0238     if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN))
0239         die("Unsupported ELF header type\n");
0240 
0241     if (ehdr.e_machine != ELF_MACHINE)
0242         die("Not for %s\n", ELF_MACHINE_NAME);
0243 
0244     if (ehdr.e_version != EV_CURRENT)
0245         die("Unknown ELF version\n");
0246 
0247     if (ehdr.e_ehsize != sizeof(Elf_Ehdr))
0248         die("Bad Elf header size\n");
0249 
0250     if (ehdr.e_phentsize != sizeof(Elf_Phdr))
0251         die("Bad program header entry\n");
0252 
0253     if (ehdr.e_shentsize != sizeof(Elf_Shdr))
0254         die("Bad section header entry\n");
0255 
0256     if (ehdr.e_shstrndx >= ehdr.e_shnum)
0257         die("String table index out of bounds\n");
0258 }
0259 
0260 static void read_shdrs(FILE *fp)
0261 {
0262     int i;
0263     Elf_Shdr shdr;
0264 
0265     secs = calloc(ehdr.e_shnum, sizeof(struct section));
0266     if (!secs)
0267         die("Unable to allocate %d section headers\n", ehdr.e_shnum);
0268 
0269     if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
0270         die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));
0271 
0272     for (i = 0; i < ehdr.e_shnum; i++) {
0273         struct section *sec = &secs[i];
0274 
0275         sec->shdr_offset = ftell(fp);
0276         if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
0277             die("Cannot read ELF section headers %d/%d: %s\n",
0278                 i, ehdr.e_shnum, strerror(errno));
0279         sec->shdr.sh_name      = elf_word_to_cpu(shdr.sh_name);
0280         sec->shdr.sh_type      = elf_word_to_cpu(shdr.sh_type);
0281         sec->shdr.sh_flags     = elf_xword_to_cpu(shdr.sh_flags);
0282         sec->shdr.sh_addr      = elf_addr_to_cpu(shdr.sh_addr);
0283         sec->shdr.sh_offset    = elf_off_to_cpu(shdr.sh_offset);
0284         sec->shdr.sh_size      = elf_xword_to_cpu(shdr.sh_size);
0285         sec->shdr.sh_link      = elf_word_to_cpu(shdr.sh_link);
0286         sec->shdr.sh_info      = elf_word_to_cpu(shdr.sh_info);
0287         sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
0288         sec->shdr.sh_entsize   = elf_xword_to_cpu(shdr.sh_entsize);
0289         if (sec->shdr.sh_link < ehdr.e_shnum)
0290             sec->link = &secs[sec->shdr.sh_link];
0291     }
0292 }
0293 
0294 static void read_strtabs(FILE *fp)
0295 {
0296     int i;
0297 
0298     for (i = 0; i < ehdr.e_shnum; i++) {
0299         struct section *sec = &secs[i];
0300 
0301         if (sec->shdr.sh_type != SHT_STRTAB)
0302             continue;
0303 
0304         sec->strtab = malloc(sec->shdr.sh_size);
0305         if (!sec->strtab)
0306             die("malloc of %d bytes for strtab failed\n",
0307                 sec->shdr.sh_size);
0308 
0309         if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0)
0310             die("Seek to %d failed: %s\n",
0311                 sec->shdr.sh_offset, strerror(errno));
0312 
0313         if (fread(sec->strtab, 1, sec->shdr.sh_size, fp) !=
0314             sec->shdr.sh_size)
0315             die("Cannot read symbol table: %s\n", strerror(errno));
0316     }
0317 }
0318 
0319 static void read_symtabs(FILE *fp)
0320 {
0321     int i, j;
0322 
0323     for (i = 0; i < ehdr.e_shnum; i++) {
0324         struct section *sec = &secs[i];
0325         if (sec->shdr.sh_type != SHT_SYMTAB)
0326             continue;
0327 
0328         sec->symtab = malloc(sec->shdr.sh_size);
0329         if (!sec->symtab)
0330             die("malloc of %d bytes for symtab failed\n",
0331                 sec->shdr.sh_size);
0332 
0333         if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0)
0334             die("Seek to %d failed: %s\n",
0335                 sec->shdr.sh_offset, strerror(errno));
0336 
0337         if (fread(sec->symtab, 1, sec->shdr.sh_size, fp) !=
0338             sec->shdr.sh_size)
0339             die("Cannot read symbol table: %s\n", strerror(errno));
0340 
0341         for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
0342             Elf_Sym *sym = &sec->symtab[j];
0343 
0344             sym->st_name  = elf_word_to_cpu(sym->st_name);
0345             sym->st_value = elf_addr_to_cpu(sym->st_value);
0346             sym->st_size  = elf_xword_to_cpu(sym->st_size);
0347             sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
0348         }
0349     }
0350 }
0351 
0352 static void read_relocs(FILE *fp)
0353 {
0354     static unsigned long base;
0355     int i, j;
0356 
0357     if (!base) {
0358         struct section *sec = sec_lookup(".text");
0359 
0360         if (!sec)
0361             die("Could not find .text section\n");
0362 
0363         base = sec->shdr.sh_addr;
0364     }
0365 
0366     for (i = 0; i < ehdr.e_shnum; i++) {
0367         struct section *sec = &secs[i];
0368 
0369         if (sec->shdr.sh_type != SHT_REL_TYPE)
0370             continue;
0371 
0372         sec->reltab = malloc(sec->shdr.sh_size);
0373         if (!sec->reltab)
0374             die("malloc of %d bytes for relocs failed\n",
0375                 sec->shdr.sh_size);
0376 
0377         if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0)
0378             die("Seek to %d failed: %s\n",
0379                 sec->shdr.sh_offset, strerror(errno));
0380 
0381         if (fread(sec->reltab, 1, sec->shdr.sh_size, fp) !=
0382             sec->shdr.sh_size)
0383             die("Cannot read symbol table: %s\n", strerror(errno));
0384 
0385         for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
0386             Elf_Rel *rel = &sec->reltab[j];
0387 
0388             rel->r_offset = elf_addr_to_cpu(rel->r_offset);
0389             /* Set offset into kernel image */
0390             rel->r_offset -= base;
0391 #if (ELF_BITS == 32)
0392             rel->r_info   = elf_xword_to_cpu(rel->r_info);
0393 #else
0394             /* Convert MIPS64 RELA format - only the symbol
0395              * index needs converting to native endianness
0396              */
0397             rel->r_info   = rel->r_info;
0398             ELF_R_SYM(rel->r_info) = elf32_to_cpu(ELF_R_SYM(rel->r_info));
0399 #endif
0400 #if (SHT_REL_TYPE == SHT_RELA)
0401             rel->r_addend = elf_xword_to_cpu(rel->r_addend);
0402 #endif
0403         }
0404     }
0405 }
0406 
0407 static void remove_relocs(FILE *fp)
0408 {
0409     int i;
0410     Elf_Shdr shdr;
0411 
0412     for (i = 0; i < ehdr.e_shnum; i++) {
0413         struct section *sec = &secs[i];
0414 
0415         if (sec->shdr.sh_type != SHT_REL_TYPE)
0416             continue;
0417 
0418         if (fseek(fp, sec->shdr_offset, SEEK_SET) < 0)
0419             die("Seek to %d failed: %s\n",
0420                 sec->shdr_offset, strerror(errno));
0421 
0422         if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
0423             die("Cannot read ELF section headers %d/%d: %s\n",
0424                 i, ehdr.e_shnum, strerror(errno));
0425 
0426         /* Set relocation section size to 0, effectively removing it.
0427          * This is necessary due to lack of support for relocations
0428          * in objcopy when creating 32bit elf from 64bit elf.
0429          */
0430         shdr.sh_size = 0;
0431 
0432         if (fseek(fp, sec->shdr_offset, SEEK_SET) < 0)
0433             die("Seek to %d failed: %s\n",
0434                 sec->shdr_offset, strerror(errno));
0435 
0436         if (fwrite(&shdr, sizeof(shdr), 1, fp) != 1)
0437             die("Cannot write ELF section headers %d/%d: %s\n",
0438                 i, ehdr.e_shnum, strerror(errno));
0439     }
0440 }
0441 
0442 static void add_reloc(struct relocs *r, uint32_t offset, unsigned type)
0443 {
0444     /* Relocation representation in binary table:
0445      * |76543210|76543210|76543210|76543210|
0446      * |  Type  |  offset from _text >> 2  |
0447      */
0448     offset >>= 2;
0449     if (offset > 0x00FFFFFF)
0450         die("Kernel image exceeds maximum size for relocation!\n");
0451 
0452     offset = (offset & 0x00FFFFFF) | ((type & 0xFF) << 24);
0453 
0454     if (r->count == r->size) {
0455         unsigned long newsize = r->size + 50000;
0456         void *mem = realloc(r->offset, newsize * sizeof(r->offset[0]));
0457 
0458         if (!mem)
0459             die("realloc failed\n");
0460 
0461         r->offset = mem;
0462         r->size = newsize;
0463     }
0464     r->offset[r->count++] = offset;
0465 }
0466 
0467 static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
0468             Elf_Sym *sym, const char *symname))
0469 {
0470     int i;
0471 
0472     /* Walk through the relocations */
0473     for (i = 0; i < ehdr.e_shnum; i++) {
0474         char *sym_strtab;
0475         Elf_Sym *sh_symtab;
0476         struct section *sec_applies, *sec_symtab;
0477         int j;
0478         struct section *sec = &secs[i];
0479 
0480         if (sec->shdr.sh_type != SHT_REL_TYPE)
0481             continue;
0482 
0483         sec_symtab  = sec->link;
0484         sec_applies = &secs[sec->shdr.sh_info];
0485         if (!(sec_applies->shdr.sh_flags & SHF_ALLOC))
0486             continue;
0487 
0488         sh_symtab = sec_symtab->symtab;
0489         sym_strtab = sec_symtab->link->strtab;
0490         for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
0491             Elf_Rel *rel = &sec->reltab[j];
0492             Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
0493             const char *symname = sym_name(sym_strtab, sym);
0494 
0495             process(sec, rel, sym, symname);
0496         }
0497     }
0498 }
0499 
0500 static int do_reloc(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
0501               const char *symname)
0502 {
0503     unsigned r_type = ELF_R_TYPE(rel->r_info);
0504     unsigned bind = ELF_ST_BIND(sym->st_info);
0505 
0506     if ((bind == STB_WEAK) && (sym->st_value == 0)) {
0507         /* Don't relocate weak symbols without a target */
0508         return 0;
0509     }
0510 
0511     if (regex_skip_reloc(symname))
0512         return 0;
0513 
0514     switch (r_type) {
0515     case R_MIPS_NONE:
0516     case R_MIPS_REL32:
0517     case R_MIPS_PC16:
0518     case R_MIPS_PC21_S2:
0519     case R_MIPS_PC26_S2:
0520         /*
0521          * NONE can be ignored and PC relative relocations don't
0522          * need to be adjusted.
0523          */
0524     case R_MIPS_HIGHEST:
0525     case R_MIPS_HIGHER:
0526         /* We support relocating within the same 4Gb segment only,
0527          * thus leaving the top 32bits unchanged
0528          */
0529     case R_MIPS_LO16:
0530         /* We support relocating by 64k jumps only
0531          * thus leaving the bottom 16bits unchanged
0532          */
0533         break;
0534 
0535     case R_MIPS_64:
0536     case R_MIPS_32:
0537     case R_MIPS_26:
0538     case R_MIPS_HI16:
0539         add_reloc(&relocs, rel->r_offset, r_type);
0540         break;
0541 
0542     default:
0543         die("Unsupported relocation type: %s (%d)\n",
0544             rel_type(r_type), r_type);
0545         break;
0546     }
0547 
0548     return 0;
0549 }
0550 
0551 static int write_reloc_as_bin(uint32_t v, FILE *f)
0552 {
0553     unsigned char buf[4];
0554 
0555     v = cpu_to_elf32(v);
0556 
0557     memcpy(buf, &v, sizeof(uint32_t));
0558     return fwrite(buf, 1, 4, f);
0559 }
0560 
0561 static int write_reloc_as_text(uint32_t v, FILE *f)
0562 {
0563     int res;
0564 
0565     res = fprintf(f, "\t.long 0x%08"PRIx32"\n", v);
0566     if (res < 0)
0567         return res;
0568     else
0569         return sizeof(uint32_t);
0570 }
0571 
0572 static void emit_relocs(int as_text, int as_bin, FILE *outf)
0573 {
0574     int i;
0575     int (*write_reloc)(uint32_t, FILE *) = write_reloc_as_bin;
0576     int size = 0;
0577     int size_reserved;
0578     struct section *sec_reloc;
0579 
0580     sec_reloc = sec_lookup(".data.reloc");
0581     if (!sec_reloc)
0582         die("Could not find relocation section\n");
0583 
0584     size_reserved = sec_reloc->shdr.sh_size;
0585 
0586     /* Collect up the relocations */
0587     walk_relocs(do_reloc);
0588 
0589     /* Print the relocations */
0590     if (as_text) {
0591         /* Print the relocations in a form suitable that
0592          * gas will like.
0593          */
0594         printf(".section \".data.reloc\",\"a\"\n");
0595         printf(".balign 4\n");
0596         /* Output text to stdout */
0597         write_reloc = write_reloc_as_text;
0598         outf = stdout;
0599     } else if (as_bin) {
0600         /* Output raw binary to stdout */
0601         outf = stdout;
0602     } else {
0603         /* Seek to offset of the relocation section.
0604         * Each relocation is then written into the
0605         * vmlinux kernel image.
0606         */
0607         if (fseek(outf, sec_reloc->shdr.sh_offset, SEEK_SET) < 0) {
0608             die("Seek to %d failed: %s\n",
0609                 sec_reloc->shdr.sh_offset, strerror(errno));
0610         }
0611     }
0612 
0613     for (i = 0; i < relocs.count; i++)
0614         size += write_reloc(relocs.offset[i], outf);
0615 
0616     /* Print a stop, but only if we've actually written some relocs */
0617     if (size)
0618         size += write_reloc(0, outf);
0619 
0620     if (size > size_reserved)
0621         /* Die, but suggest a value for CONFIG_RELOCATION_TABLE_SIZE
0622          * which will fix this problem and allow a bit of headroom
0623          * if more kernel features are enabled
0624          */
0625         die("Relocations overflow available space!\n" \
0626             "Please adjust CONFIG_RELOCATION_TABLE_SIZE " \
0627             "to at least 0x%08x\n", (size + 0x1000) & ~0xFFF);
0628 }
0629 
0630 /*
0631  * As an aid to debugging problems with different linkers
0632  * print summary information about the relocs.
0633  * Since different linkers tend to emit the sections in
0634  * different orders we use the section names in the output.
0635  */
0636 static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
0637                 const char *symname)
0638 {
0639     printf("%16s  0x%08x  %16s  %40s  %16s\n",
0640         sec_name(sec->shdr.sh_info),
0641         (unsigned int)rel->r_offset,
0642         rel_type(ELF_R_TYPE(rel->r_info)),
0643         symname,
0644         sec_name(sym->st_shndx));
0645     return 0;
0646 }
0647 
0648 static void print_reloc_info(void)
0649 {
0650     printf("%16s  %10s  %16s  %40s  %16s\n",
0651         "reloc section",
0652         "offset",
0653         "reloc type",
0654         "symbol",
0655         "symbol section");
0656     walk_relocs(do_reloc_info);
0657 }
0658 
0659 #if ELF_BITS == 64
0660 # define process process_64
0661 #else
0662 # define process process_32
0663 #endif
0664 
0665 void process(FILE *fp, int as_text, int as_bin,
0666          int show_reloc_info, int keep_relocs)
0667 {
0668     regex_init();
0669     read_ehdr(fp);
0670     read_shdrs(fp);
0671     read_strtabs(fp);
0672     read_symtabs(fp);
0673     read_relocs(fp);
0674     if (show_reloc_info) {
0675         print_reloc_info();
0676         return;
0677     }
0678     emit_relocs(as_text, as_bin, fp);
0679     if (!keep_relocs)
0680         remove_relocs(fp);
0681 }