This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Check that Elf_Rela addends are always written with -r
ClosedPublic

Authored by arichardson on Feb 1 2018, 5:01 AM.

Details

Summary

The fix was committed as part of rL324064. This adds an x86 testcase

Event Timeline

arichardson created this revision.Feb 1 2018, 5:01 AM

So this intruduces -relocation-format=xxx option.
Is it documented somewhere ? I did not find the description.

Looks it is for MIPS only ? I wonder why do you want to
mix .rel and .rel[a] objects and if it is really legal thing to do first of all ?

+Simon, he knows about MIPS much more than me.

I originally discovered this issue because in our CHERI fork of LLD we set Config->IsRela = false when linking MIPS64 code. We do this because the FreeBSD MIPS rtld only understands Elf_Rel relocations and does not process Elf_Rela.

The issue here is not speicific to MIPS. It only depends on an input file containing Elf_Rela for a target that has Elf_Rel as the default output format.

I added the command line flag since this makes it possible to test this issue without fabricating .o files. Additionally, it will make it possible to use LLD for operating systems where rtld doesn't use Rela and the IsRela = Config->Is64 || IsX32 || Config->MipsN32Abi || Machine == EM_PPC. check is not correct.

grimar added a comment.Feb 1 2018, 6:51 AM

I originally discovered this issue because in our CHERI fork of LLD we set Config->IsRela = false when linking MIPS64 code. We do this because the FreeBSD MIPS rtld only understands Elf_Rel relocations and does not process Elf_Rela.

It sounds like FreeBSD MIPS rtld bug honestly and probably should be fixed on their side.

I guess it currently works with GNU linkers because they always write the addend to output, even when output is .rela.
We faced something like that before when worked on x64 target. And it was fixed on FreeBSD side.

I do not think we want to add command line option like that in LLD. One possible option in theory would be to have
the same behavior as gnu linkers do, but it is also unclear it is needed, because seem helps to hide an issue instead of fixing it.

Lets wait to hear what other people think.

I originally discovered this issue because in our CHERI fork of LLD we set Config->IsRela = false when linking MIPS64 code. We do this because the FreeBSD MIPS rtld only understands Elf_Rel relocations and does not process Elf_Rela.

It sounds like FreeBSD MIPS rtld bug honestly and probably should be fixed on their side.

MIPS O32 ABI uses the Elf_Rel format in object files, while N32 and N64 ABIs uses the Elf_Rela. But originally in case of all ABIs output relocations in executable files and DSO are written in the Elf_Rel format. I think the goal was to reduce a file size. In case of embedded systems with small amount of memory it can be a benefit. Maybe there are some other reasons. When I implement MIPS LLD backend I used the same format for input and output files, i.e. Elf_Rel for O32 ABI and Elf_Rela for N32 and N64 ABIs. It works for Linux because the dynamic linker accepts all formats. It looks like FreeBSD linker is more stricter.

arichardson added a comment.EditedFeb 1 2018, 7:21 AM

I guess it currently works with GNU linkers because they always write the addend to output, even when output is .rela.
We faced something like that before when worked on x64 target. And it was fixed on FreeBSD side.

I have been working on adding RELA support to the mips rtld but this is quite annoying and will take a while. For now I have been able to run code just by setting Config->IsRela to false.

I don't know if the MIPS specifies that Elf_Rela should to be used for n64. I believe this choice is up to the operating system but I could be wrong.

I do not think we want to add command line option like that in LLD. One possible option in theory would be to have
the same behavior as gnu linkers do, but it is also unclear it is needed, because seem helps to hide an issue instead of fixing it.

This is actually fixing an issue since compilers other than clang are not required to use the same relocation kind as LLD for the object files.
I can also add a patch to completely reject any Elf_Rel relocations if Config->IsRela is set but I don't think that is the right way forward. Right now we are producing broken object files if this is the case and I would very much like to avoid that.

grimar added a comment.Feb 1 2018, 7:50 AM

I originally discovered this issue because in our CHERI fork of LLD we set Config->IsRela = false when linking MIPS64 code. We do this because the FreeBSD MIPS rtld only understands Elf_Rel relocations and does not process Elf_Rela.

It sounds like FreeBSD MIPS rtld bug honestly and probably should be fixed on their side.

MIPS O32 ABI uses the Elf_Rel format in object files, while N32 and N64 ABIs uses the Elf_Rela. But originally in case of all ABIs output relocations in executable files and DSO are written in the Elf_Rel format. I think the goal was to reduce a file size. In case of embedded systems with small amount of memory it can be a benefit. Maybe there are some other reasons. When I implement MIPS LLD backend I used the same format for input and output files, i.e. Elf_Rel for O32 ABI and Elf_Rela for N32 and N64 ABIs. It works for Linux because the dynamic linker accepts all formats. It looks like FreeBSD linker is more stricter.

So should/can we switch LLD to always emit Elf_Rel for all MIPS outputs then ?

ruiu added a comment.Feb 1 2018, 12:46 PM

So should/can we switch LLD to always emit Elf_Rel for all MIPS outputs then ?

I think I agree with George. REL/RELA is defined by the ABI and making it configurable seems odd.

In D42790#995391, @ruiu wrote:

So should/can we switch LLD to always emit Elf_Rel for all MIPS outputs then ?

I think I agree with George. REL/RELA is defined by the ABI and making it configurable seems odd.

I only added the flag to make the testing easier. I can try to create the various input files using yaml2obj but that will require 4 different input files instead of just one .s file.

ruiu added a comment.Feb 1 2018, 1:16 PM

Please add test files to test your code instead of adding a user-visible command line flag. User-visible flags shouldn't be added without careful planning.

arichardson retitled this revision from [ELF] Ensure that Elf_Rela addends are always written with -r to [ELF] Check that Elf_Rela addends are always written with -r.
arichardson edited the summary of this revision. (Show Details)

Since the patch was committed change this to only a test case (depends on D42839)

ruiu accepted this revision.Feb 2 2018, 12:47 PM

LGTM

This revision is now accepted and ready to land.Feb 2 2018, 12:47 PM
espindola edited reviewers, added: espindola; removed: rafael.Mar 14 2018, 4:52 PM
atanasyan closed this revision.Dec 12 2018, 9:11 AM

Is this patch still needs to be applied?

Is this patch still needs to be applied?

Probably not. I could rewrite the different inputs as YAML since the llvm-mc change doesn't seem that useful.