Skip to content

Commit

Permalink
[ELF] Add explictly exported atoms and export R_*_COPY'ed atoms.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bigcheese committed Oct 3, 2013
1 parent 94797ae commit 3e18eab
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
14 changes: 14 additions & 0 deletions include/lld/ReaderWriter/ELFLinkingContext.h
Expand Up @@ -23,6 +23,7 @@
#include "llvm/Support/ELF.h"

#include <memory>
#include <unordered_set>

namespace lld {
class DefinedAtom;
Expand Down Expand Up @@ -206,6 +207,15 @@ class ELFLinkingContext : public LinkingContext {
return _rpathLinkList;
}

void addExplicitlyExportedAtom(const DefinedAtom *a) {
_explicitlyExportedAtoms.insert(a);
}

const std::unordered_set<const DefinedAtom *> &
getExplicitlyExportedAtoms() const {
return _explicitlyExportedAtoms;
}

private:
ELFLinkingContext() LLVM_DELETED_FUNCTION;

Expand Down Expand Up @@ -241,6 +251,10 @@ class ELFLinkingContext : public LinkingContext {
StringRef _soname;
StringRefVector _rpathList;
StringRefVector _rpathLinkList;

/// \brief The set of explicitly exported atoms which should be added to the
/// dynamic symbol table.
std::unordered_set<const DefinedAtom *> _explicitlyExportedAtoms;
};
} // end namespace lld

Expand Down
8 changes: 8 additions & 0 deletions lib/ReaderWriter/ELF/OutputELFWriter.h
Expand Up @@ -154,6 +154,14 @@ void OutputELFWriter<ELFT>::buildDynamicSymbolTable(const File &file) {
_dynamicSymbolTable->addSymbol(sla, ELF::SHN_UNDEF);
_soNeeded.insert(sla->loadName());
}
for (auto sec : this->_layout->sections())
if (auto section = dyn_cast<AtomSection<ELFT>>(sec))
for (const auto &atom : section->atoms()) {
const DefinedAtom *da = dyn_cast<const DefinedAtom>(atom->_atom);
if (da && _context.getExplicitlyExportedAtoms().count(da))
this->_dynamicSymbolTable->addSymbol(atom->_atom, section->ordinal(),
atom->_virtualAddr, atom);
}
for (const auto &loadName : _soNeeded) {
Elf_Dyn dyn;
dyn.d_tag = DT_NEEDED;
Expand Down
6 changes: 4 additions & 2 deletions lib/ReaderWriter/ELF/X86_64/X86_64LinkingContext.cpp
Expand Up @@ -206,7 +206,7 @@ template <class Derived> class GOTPLTPass : public Pass {

public:
GOTPLTPass(const ELFLinkingContext &ctx)
: _file(ctx), _null(nullptr), _PLT0(nullptr), _got0(nullptr),
: _file(ctx), _ctx(ctx), _null(nullptr), _PLT0(nullptr), _got0(nullptr),
_got1(nullptr) {}

/// \brief Do the pass.
Expand Down Expand Up @@ -257,6 +257,7 @@ template <class Derived> class GOTPLTPass : public Pass {
protected:
/// \brief Owner of all the Atoms created by this pass.
ELFPassFile _file;
const ELFLinkingContext &_ctx;

/// \brief Map Atoms to their GOT entries.
llvm::DenseMap<const Atom *, GOTAtom *> _gotMap;
Expand Down Expand Up @@ -371,13 +372,14 @@ class DynamicGOTPLTPass LLVM_FINAL : public GOTPLTPass<DynamicGOTPLTPass> {
return obj->second;

auto oa = new (_file._alloc) ObjectAtom(_file);
oa->addReference(R_X86_64_COPY, 0, a, 0);
oa->addReference(R_X86_64_COPY, 0, oa, 0);

oa->_name = a->name();
oa->_size = a->size();

_objectMap[a] = oa;
_objectVector.push_back(oa);
const_cast<ELFLinkingContext &>(_ctx).addExplicitlyExportedAtom(oa);
return oa;
}

Expand Down
14 changes: 12 additions & 2 deletions test/elf/undef-from-main-dso.test
@@ -1,8 +1,8 @@
RUN: lld -flavor gnu -e main -o %t -L%p/Inputs -lundef %p/Inputs/undef.o
RUN: llvm-readobj -relocations -symbols %t | FileCheck %s
RUN: llvm-readobj -relocations -symbols -dyn-symbols %t | FileCheck %s

RUN: lld -flavor gnu -e main -o %t -L%p/Inputs -lundef %p/Inputs/undef-pc32.o
RUN: llvm-readobj -relocations -symbols %t | FileCheck %s
RUN: llvm-readobj -relocations -symbols -dyn-symbols %t | FileCheck %s

# DSO source code:
# int x[2] = { 1, 2 };
Expand Down Expand Up @@ -30,3 +30,13 @@ CHECK-NEXT: Binding: Global (0x1)
CHECK-NEXT: Type: Object (0x1)
CHECK-NEXT: Other: 0
CHECK-NEXT: Section: .bss

CHECK: Name: x@ ({{[0-9]+}}

CHECK: Name: x@ ({{[0-9]+}}
CHECK-NEXT: Value: 0x{{[1-9A-F][0-9A-F]*}}
CHECK-NEXT: Size: 8
CHECK-NEXT: Binding: Global (0x1)
CHECK-NEXT: Type: Object (0x1)
CHECK-NEXT: Other: 0
CHECK-NEXT: Section: .bss

0 comments on commit 3e18eab

Please sign in to comment.