This is an archive of the discontinued LLVM Phabricator instance.

Avoid code duplication when creating dynamic relocations
ClosedPublic

Authored by rafael on Feb 4 2016, 7:46 AM.

Details

Reviewers
ruiu
Summary

Another case where we currently have almost duplicated code is the creation of dynamic relocations. First to decide if we need one, then to decide what to write.

This patch fixes it by passing more information from the relocation scan to the section writing code. This is the same idea used for r258723.

I actually think it should be possible to simplify this further by reordering things a bit in the writer. For example, we should be able to represent almost every position in the file with an OutputSeciton and offset. When writing it out we then just need to add the offset to the OutputSection VA.

Diff Detail

Event Timeline

rafael updated this revision to Diff 46917.Feb 4 2016, 7:46 AM
rafael retitled this revision from to Avoid code duplication when creating dynamic relocations.
rafael updated this object.
rafael added a reviewer: ruiu.
rafael set the repository for this revision to rL LLVM.
rafael added a subscriber: llvm-commits.

Forgot to mention: This patch has a small impact on performance. On linux on a mac pro the times I get for linking clang and scylla are:

clang
master 0.568085717s ( +- 0.12% )
patch 0.568659432s ( +- 0.05% ) 1.0010099092141056 times slower

scylla
master 3.883971842s ( +- 0.11% )
patch 3.886440224s ( +- 0.08% ) 1.0006355303540846 times slower

grimar added a subscriber: grimar.Feb 4 2016, 8:38 AM
grimar added inline comments.
ELF/OutputSections.cpp
275

Don't you need some kind of "unknown reloc type" assert here ?

ELF/OutputSections.h
188

Not all comments have a dot at the end.

ELF/Writer.cpp
949

Was that intentionaly moved ?

ruiu edited edge metadata.Feb 4 2016, 9:19 AM

Looks like this is towards the direction that I wanted to see. Thank you for doing this. First round of review.

ELF/OutputSections.cpp
257–260

This is a cute way to write a switch-case without lots of "break"s, but I would probably make this a regular static function in this file. Or, you may want to make it a member function of DynamicReloc?

260

You may want to define a variable of type SymbolBody * for Rel.Sym, so that you need to repeat Rel.Sym many times in this function.

ELF/OutputSections.h
206

I'd add newlines between constructors.

ELF/Writer.cpp
420–421

Could you update this comment for me?

420–453

Flip the condition and use early return.

427

I'd use early return instead of else if or else.

ruiu added inline comments.Feb 4 2016, 10:08 AM
ELF/Writer.cpp
234–238

I like this part. It naturally represents what we are doing for the TLS relocations (rather than reserving an extra slot in an obscure way.)

rafael updated this revision to Diff 46947.Feb 4 2016, 12:25 PM
rafael edited edge metadata.
rafael removed rL LLVM as the repository for this revision.

Address review comments.

ruiu accepted this revision.Feb 4 2016, 1:16 PM
ruiu edited edge metadata.

LGTM with a few nits.

ELF/OutputSections.cpp
234–236

You can now remove "template ".

250

Remove "template "?

258

Is this equivalent to

uint32_t SymIdx = Rel.UseSymVA ? 0 : Sym->DynsymIndex;

? (You seem to assume that if UseSymVA is true, Rel.Sym is not false in a few lines above this one.)

This revision is now accepted and ready to land.Feb 4 2016, 1:16 PM

Comment at: ELF/OutputSections.cpp:258
@@ +257,3 @@
+ P->r_offset = getOffset(Rel);
+ uint32_t SymIdx = (!Rel.UseSymVA && Sym) ? Sym->DynsymIndex : 0;

+ P->setSymbolAndType(SymIdx, Rel.Type, Config->Mips64EL);

Is this equivalent to

uint32_t SymIdx = Rel.UseSymVA ? 0 : Sym->DynsymIndex;

? (You seem to assume that if UseSymVA is true, Rel.Sym is not false in a few lines above this one.)

It is not. When UseSymVA is false Sym can be null.

Cheers,
Rafael