This is an archive of the discontinued LLVM Phabricator instance.

[ELF][MIPS] Produce a correct and complete set of MIPS ELF header flags
ClosedPublic

Authored by atanasyan on Aug 4 2016, 5:39 AM.

Details

Summary

The patch extends the getMipsEFlags function. Now in that function we iterate over all object files, parse ELF header flags and merge them. If a file is incompatible with previously analyzed ones we show an error or warning. That can happen if, for example, we try to link files with incompatible ABI, ISA, NAN encoding etc.

There is an alternative solution. We can check and merge flags and reject incompatible input modules in the isCompatible function which is called from the SymbolTable::addFile method. But in that case we have to save and keep somewhere a merged ELF flags combination to use it later in the writer.

Diff Detail

Repository
rL LLVM

Event Timeline

atanasyan updated this revision to Diff 66791.Aug 4 2016, 5:39 AM
atanasyan retitled this revision from to [ELF][MIPS] Produce a correct and complete set of MIPS ELF header flags.
atanasyan updated this object.
atanasyan added reviewers: ruiu, rafael.
atanasyan set the repository for this revision to rL LLVM.
atanasyan added a project: lld.
atanasyan added a subscriber: llvm-commits.
ruiu added inline comments.Aug 4 2016, 1:42 PM
ELF/Writer.cpp
1242–1262 ↗(On Diff #66791)

I cannot find a simple rule for this code. What is the expected output for what inputs? Is there a documentation to describe the rule behind it?

atanasyan added inline comments.Aug 4 2016, 3:39 PM
ELF/Writer.cpp
1242–1262 ↗(On Diff #66791)

I cannot find a simple rule for this code. What is the expected output for what inputs? Is there a documentation to describe the rule behind it?

As usual there is no documentation on this code. The general idea - ELF flags define ISA (R_MIPS_32, R_MIPS_32R2, R_MIPS_64 ...), ABI (R_MIPS_O32, R_MIPS_ABI2 ...) and features flags (R_MIPS_NAN2008, R_MIPS_PIC ...). Some kinds of ISAs are compatible with each other and we need to select the "highest" from input ISAs. For example, if one input file has R_MIPS_32 ISA and another one has R_MIPS_32R2 ISA, we need to put R_MIPS_32R2 into the output file. SOme kinds of ISAs are incompatible. We should reject attempts to link R_MIPS_32R2 and R_MIPS_32R6 ISAs together. The same logic is applicable to ABI and some features flags like R_MIPS_NAN2008 and R_MIPS_FP64. Other features flags like EF_MIPS_NOREORDER, EF_MIPS_MICROMIPS etc can be just merged.

As to the R_MIPS_PIC and R_MIPS_CPIC flags - PIC code is inherently CPIC and may not set CPIC flag explicitly. But it is a good practice to set it in the output file so we do that. If we merge PIC and non-PIC code, output file cannot be considered as position independent so we clear PIC flag if as soon as we see non-PIC input file. If input file does not have PIC not CPIC flags that means that this file does not follow abicalls convention and we show a warning if linker mix abicalls and non-abicalls files.

ruiu added inline comments.Aug 4 2016, 4:07 PM
ELF/Writer.cpp
1184 ↗(On Diff #66791)

This and the following two "tostring" functions seem a bit odd to be here in the linker. Don't you think they should actually live in LLVM instead of LLD?

1242–1262 ↗(On Diff #66791)

Thank you for the explanation. So it seems to be inevitably complicated. Do you think you can split this function into small functions, each of which checks only one thing? I'd imagine we could have getMipsPicFlag(), getMipsArch(), getMipsAbi(), etc.

atanasyan updated this revision to Diff 66960.Aug 5 2016, 8:24 AM

Thanks for review.

In this patch I moved a code from getMipsEFlags into some small functions to check and update ISA, ABI, PIC, and features flags separately.

I like the idea to move "tostring" function into LLVM code base. But I cannot find the right place for them. Any ideas?

ruiu accepted this revision.Aug 5 2016, 2:15 PM
ruiu edited edge metadata.

LGTM. As to moving the code to LLVM, I have no idea. But we can move the code later.

This revision is now accepted and ready to land.Aug 5 2016, 2:15 PM
This revision was automatically updated to reflect the committed changes.