[llvm-commits] LLD: update the WriterELF.cpp to use newly added Atom reference data

Sid Manning sidneym at codeaurora.org
Fri Sep 21 10:41:19 PDT 2012


On 09/20/12 14:56, Nick Kledzik wrote:
> On Sep 20, 2012, at 6:46 AM, Sid Manning wrote:
>> Is this change ok to commit?
> It is ok.
>
> My only suggestion is to use a struct instead of a tuple for AtomInfo.  Tuples are nice for on-the-fly types, but you are defining a typedef for AtomInfo, so why have named fields?
>
> typedef std::tuple<const DefinedAtom*, uint64_t>  AtomInfo;
>
> You current use look like:
>    std::get<0>(ai)
> whereas a struct would require less mental buffering to understand:
>     ai->atom
>
I just as talked with Hemant and this was added by Michael during some 
of their exchanges.  In this case I'm a user and I agree it takes some 
back and forth to use the tuple.

On another issue in doing the applyFixup for Hexagon I'm using the 
relocation enums from ELF.h.  I hope this will turn out to be ok.  The 
only problem I have at this point is the kindToString and stringToKind. 
  ELF.h has a getRelocationTypeName function but I don't see an easy way 
to make use of that function from kindToString.

Thanks,



> -Nick
>
>
>>
>> On 09/18/12 09:14, Sid Manning wrote:
>>> This patch makes use of recently added relocation reference data.  The
>>> bulk of this is derived from the Mach-O writer.
>>>
>>>   * Adds loop to SectionChunk::write traverse references calling the
>>> writer's fixup handler, applyFixup.
>>>   * Adds method, ELFWriter::buildAtomToAddressMap to that creates a
>>> mapping from an atom to its runtime address.
>>>   * Adds method, ELFWriter::addressOfAtom to return the runtime address
>>> of the atom.
>>>
>>>
>>> Thanks,
>>>
>>> --
>>> Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
>>> hosted by The Linux Foundation
>>>
>>>
>>> WriterELF.diff
>>>
>>>
>>>
>>> This patch makes use of recently added relocation reference data.
>>>
>>>   * Adds loop to SectionChunk::write traverse references calling the
>>>     writer's fixup handler, applyFixup.
>>>   * Adds method, ELFWriter::buildAtomToAddressMap to that creates a mapping
>>>     from an atom to its runtime address.
>>>   * Adds method, ELFWriter::addressOfAtom to return the runtime address of
>>>     the atom.
>>>
>>> Index: lib/ReaderWriter/ELF/WriterELF.cpp
>>> ===================================================================
>>> --- lib/ReaderWriter/ELF/WriterELF.cpp	(revision 164027)
>>> +++ lib/ReaderWriter/ELF/WriterELF.cpp	(working copy)
>>> @@ -425,7 +425,20 @@
>>>         continue;
>>>       uint8_t *atomContent = chunkBuffer + std::get<1>(ai);
>>>       std::copy_n(content.data(), contentSize, atomContent);
>>> -    // TODO Apply fixups to file buffer
>>> +
>>> +    for (const Reference *ref : *std::get<0>(ai)){
>>> +      uint32_t offset = ref->offsetInAtom();
>>> +      uint64_t targetAddress = 0;
>>> +
>>> +      if ( ref->target() != nullptr )
>>> +         targetAddress = _writer.addressOfAtom(ref->target());
>>> +
>>> +      uint64_t fixupAddress = _writer.addressOfAtom(std::get<0>(ai)) + offset;
>>> +      _writer.kindHandler()->applyFixup(ref->kind(), ref->addend(),
>>> +&atomContent[offset],
>>> +                                        fixupAddress,
>>> +                                        targetAddress);
>>> +    }
>>>     }
>>>   }
>>>   //
>>> @@ -685,8 +698,9 @@
>>>     typedef object::Elf_Shdr_Impl<target_endianness, is64Bits>   Elf_Shdr;
>>>     ELFWriter(const WriterOptionsELF&options);
>>>     virtual error_code writeFile(const lld::File&File, StringRef path);
>>> +  uint64_t addressOfAtom(const Atom *atom);
>>>     ArrayRef<Chunk<target_endianness, is64Bits>*>   chunks() { return _chunks; }
>>> -  KindHandler *kindHandler() { return _referenceKindHandler; }
>>> +  KindHandler *kindHandler() { return _referenceKindHandler.get(); }
>>>
>>>     std::vector<SectionChunk<target_endianness, is64Bits>*>   sectionChunks() {
>>>       return _sectionChunks ;
>>> @@ -699,11 +713,18 @@
>>>   private:
>>>     void build(const lld::File&file);
>>>     void createChunks(const lld::File&file);
>>> +  void buildAtomToAddressMap();
>>>     void assignFileOffsets();
>>>     const WriterOptionsELF&_options;
>>> +
>>> +/// \brief AtomToAddress: Is a mapping from an Atom to the address where
>>> +/// it will live in the output file.
>>> +  typedef llvm::DenseMap<const Atom*, uint64_t>   AtomToAddress;
>>> +
>>>     ELFStringSectionChunk<target_endianness, is64Bits>   *_shstrtable ;
>>>     std::unique_ptr<KindHandler>   _referenceKindHandler;
>>>     ELFSectionHeaderChunk<target_endianness, is64Bits>   *_sectionHeaderChunk;
>>> +  AtomToAddress _atomToAddress;
>>>     std::vector<Chunk<target_endianness, is64Bits>*>   _chunks;
>>>     const DefinedAtom *_entryAtom;
>>>     std::vector<SectionChunk<target_endianness, is64Bits>*>   _sectionChunks;
>>> @@ -725,6 +746,7 @@
>>>     // Create objects for each chunk.
>>>     createChunks(file);
>>>     assignFileOffsets();
>>> +  buildAtomToAddressMap();
>>>   }
>>>
>>>   template<support::endianness target_endianness, bool is64Bits>
>>> @@ -794,7 +816,21 @@
>>>     _chunks.push_back(_shstrtable);
>>>   }
>>>
>>> +template<support::endianness target_endianness, bool is64Bits>
>>> +void ELFWriter<target_endianness, is64Bits>
>>> +              ::buildAtomToAddressMap () {
>>>
>>> +// _atomToAddress is a DenseMap that maps an atom its file address.
>>> +// std::get<1>(ai) is the offset from the start of the section to the atom.
>>> +  for (auto&chunk : _sectionChunks){
>>> +    for (auto&ai : chunk->atoms() ) {
>>> +      _atomToAddress[std::get<0>(ai)] = chunk->address() + std::get<1>(ai);
>>> +    }
>>> +  }
>>> +
>>> +
>>> +}
>>> +
>>>   template<support::endianness target_endianness, bool is64Bits>
>>>   void ELFWriter<target_endianness, is64Bits>::assignFileOffsets() {
>>>     DEBUG_WITH_TYPE("WriterELF-layout", dbgs()
>>> @@ -844,6 +880,11 @@
>>>     return buffer->commit();
>>>   }
>>>
>>> +template<support::endianness target_endianness, bool is64Bits>
>>> +uint64_t ELFWriter<target_endianness, is64Bits>
>>> +                    ::addressOfAtom(const Atom *atom) {
>>> +  return _atomToAddress[atom];
>>> +}
>>>   } // namespace elf
>>>
>>>   Writer *createWriterELF(const WriterOptionsELF&options) {
>>
>>
>> --
>> Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation
>


-- 
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, 
hosted by The Linux Foundation



More information about the llvm-commits mailing list