Skip to content

Commit fb41478

Browse files
Matt Helsleyjpoimboe
authored andcommitted
objtool: Add support for relocations without addends
Currently objtool only collects information about relocations with addends. In recordmcount, which we are about to merge into objtool, some supported architectures do not use rela relocations. Signed-off-by: Matt Helsley <[email protected]> Reviewed-by: Julien Thierry <[email protected]> Reviewed-by: Kamalesh Babulal <[email protected]> Signed-off-by: Josh Poimboeuf <[email protected]>
1 parent f197422 commit fb41478

File tree

3 files changed

+134
-20
lines changed

3 files changed

+134
-20
lines changed

tools/objtool/elf.c

Lines changed: 128 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,32 @@ void elf_add_reloc(struct elf *elf, struct reloc *reloc)
496496
elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc));
497497
}
498498

499+
static int read_rel_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
500+
{
501+
if (!gelf_getrel(sec->data, i, &reloc->rel)) {
502+
WARN_ELF("gelf_getrel");
503+
return -1;
504+
}
505+
reloc->type = GELF_R_TYPE(reloc->rel.r_info);
506+
reloc->addend = 0;
507+
reloc->offset = reloc->rel.r_offset;
508+
*symndx = GELF_R_SYM(reloc->rel.r_info);
509+
return 0;
510+
}
511+
512+
static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
513+
{
514+
if (!gelf_getrela(sec->data, i, &reloc->rela)) {
515+
WARN_ELF("gelf_getrela");
516+
return -1;
517+
}
518+
reloc->type = GELF_R_TYPE(reloc->rela.r_info);
519+
reloc->addend = reloc->rela.r_addend;
520+
reloc->offset = reloc->rela.r_offset;
521+
*symndx = GELF_R_SYM(reloc->rela.r_info);
522+
return 0;
523+
}
524+
499525
static int read_relocs(struct elf *elf)
500526
{
501527
struct section *sec;
@@ -505,7 +531,8 @@ static int read_relocs(struct elf *elf)
505531
unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0;
506532

507533
list_for_each_entry(sec, &elf->sections, list) {
508-
if (sec->sh.sh_type != SHT_RELA)
534+
if ((sec->sh.sh_type != SHT_RELA) &&
535+
(sec->sh.sh_type != SHT_REL))
509536
continue;
510537

511538
sec->base = find_section_by_index(elf, sec->sh.sh_info);
@@ -525,16 +552,17 @@ static int read_relocs(struct elf *elf)
525552
return -1;
526553
}
527554
memset(reloc, 0, sizeof(*reloc));
528-
529-
if (!gelf_getrela(sec->data, i, &reloc->rela)) {
530-
WARN_ELF("gelf_getrela");
531-
return -1;
555+
switch (sec->sh.sh_type) {
556+
case SHT_REL:
557+
if (read_rel_reloc(sec, i, reloc, &symndx))
558+
return -1;
559+
break;
560+
case SHT_RELA:
561+
if (read_rela_reloc(sec, i, reloc, &symndx))
562+
return -1;
563+
break;
564+
default: return -1;
532565
}
533-
534-
reloc->type = GELF_R_TYPE(reloc->rela.r_info);
535-
reloc->addend = reloc->rela.r_addend;
536-
reloc->offset = reloc->rela.r_offset;
537-
symndx = GELF_R_SYM(reloc->rela.r_info);
538566
reloc->sym = find_symbol_by_index(elf, symndx);
539567
reloc->sec = sec;
540568
if (!reloc->sym) {
@@ -722,7 +750,37 @@ struct section *elf_create_section(struct elf *elf, const char *name,
722750
return sec;
723751
}
724752

725-
struct section *elf_create_reloc_section(struct elf *elf, struct section *base)
753+
static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base)
754+
{
755+
char *relocname;
756+
struct section *sec;
757+
758+
relocname = malloc(strlen(base->name) + strlen(".rel") + 1);
759+
if (!relocname) {
760+
perror("malloc");
761+
return NULL;
762+
}
763+
strcpy(relocname, ".rel");
764+
strcat(relocname, base->name);
765+
766+
sec = elf_create_section(elf, relocname, sizeof(GElf_Rel), 0);
767+
free(relocname);
768+
if (!sec)
769+
return NULL;
770+
771+
base->reloc = sec;
772+
sec->base = base;
773+
774+
sec->sh.sh_type = SHT_REL;
775+
sec->sh.sh_addralign = 8;
776+
sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
777+
sec->sh.sh_info = base->idx;
778+
sec->sh.sh_flags = SHF_INFO_LINK;
779+
780+
return sec;
781+
}
782+
783+
static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base)
726784
{
727785
char *relocname;
728786
struct section *sec;
@@ -752,16 +810,53 @@ struct section *elf_create_reloc_section(struct elf *elf, struct section *base)
752810
return sec;
753811
}
754812

755-
int elf_rebuild_reloc_section(struct section *sec)
813+
struct section *elf_create_reloc_section(struct elf *elf,
814+
struct section *base,
815+
int reltype)
816+
{
817+
switch (reltype) {
818+
case SHT_REL: return elf_create_rel_reloc_section(elf, base);
819+
case SHT_RELA: return elf_create_rela_reloc_section(elf, base);
820+
default: return NULL;
821+
}
822+
}
823+
824+
static int elf_rebuild_rel_reloc_section(struct section *sec, int nr)
756825
{
757826
struct reloc *reloc;
758-
int nr, idx = 0, size;
759-
GElf_Rela *relocs;
827+
int idx = 0, size;
828+
GElf_Rel *relocs;
760829

761-
nr = 0;
762-
list_for_each_entry(reloc, &sec->reloc_list, list)
763-
nr++;
830+
/* Allocate a buffer for relocations */
831+
size = nr * sizeof(*relocs);
832+
relocs = malloc(size);
833+
if (!relocs) {
834+
perror("malloc");
835+
return -1;
836+
}
837+
838+
sec->data->d_buf = relocs;
839+
sec->data->d_size = size;
840+
841+
sec->sh.sh_size = size;
842+
843+
idx = 0;
844+
list_for_each_entry(reloc, &sec->reloc_list, list) {
845+
relocs[idx].r_offset = reloc->offset;
846+
relocs[idx].r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
847+
idx++;
848+
}
849+
850+
return 0;
851+
}
764852

853+
static int elf_rebuild_rela_reloc_section(struct section *sec, int nr)
854+
{
855+
struct reloc *reloc;
856+
int idx = 0, size;
857+
GElf_Rela *relocs;
858+
859+
/* Allocate a buffer for relocations with addends */
765860
size = nr * sizeof(*relocs);
766861
relocs = malloc(size);
767862
if (!relocs) {
@@ -785,6 +880,22 @@ int elf_rebuild_reloc_section(struct section *sec)
785880
return 0;
786881
}
787882

883+
int elf_rebuild_reloc_section(struct section *sec)
884+
{
885+
struct reloc *reloc;
886+
int nr;
887+
888+
nr = 0;
889+
list_for_each_entry(reloc, &sec->reloc_list, list)
890+
nr++;
891+
892+
switch (sec->sh.sh_type) {
893+
case SHT_REL: return elf_rebuild_rel_reloc_section(sec, nr);
894+
case SHT_RELA: return elf_rebuild_rela_reloc_section(sec, nr);
895+
default: return -1;
896+
}
897+
}
898+
788899
int elf_write(const struct elf *elf)
789900
{
790901
struct section *sec;

tools/objtool/elf.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ struct symbol {
6161
struct reloc {
6262
struct list_head list;
6363
struct hlist_node hash;
64-
GElf_Rela rela;
64+
union {
65+
GElf_Rela rela;
66+
GElf_Rel rel;
67+
};
6568
struct section *sec;
6669
struct symbol *sym;
6770
unsigned int type;
@@ -116,7 +119,7 @@ static inline u32 reloc_hash(struct reloc *reloc)
116119

117120
struct elf *elf_open_read(const char *name, int flags);
118121
struct section *elf_create_section(struct elf *elf, const char *name, size_t entsize, int nr);
119-
struct section *elf_create_reloc_section(struct elf *elf, struct section *base);
122+
struct section *elf_create_reloc_section(struct elf *elf, struct section *base, int reltype);
120123
void elf_add_reloc(struct elf *elf, struct reloc *reloc);
121124
int elf_write(const struct elf *elf);
122125
void elf_close(struct elf *elf);

tools/objtool/orc_gen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ int create_orc_sections(struct objtool_file *file)
181181
if (!sec)
182182
return -1;
183183

184-
ip_relocsec = elf_create_reloc_section(file->elf, sec);
184+
ip_relocsec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
185185
if (!ip_relocsec)
186186
return -1;
187187

0 commit comments

Comments
 (0)