/// It exists to represent content at fixed addresses in memory.
class AbsoluteAtom : public Atom {
public:
+
virtual uint64_t value() const = 0;
+ /// scope - The visibility of this atom to other atoms. C static functions
+ /// have scope scopeTranslationUnit. Regular C functions have scope
+ /// scopeGlobal. Functions compiled with visibility=hidden have scope
+ /// scopeLinkageUnit so they can be see by other atoms being linked but not
+ /// by the OS loader.
+ virtual Scope scope() const = 0;
+
static inline bool classof(const Atom *a) {
return a->definition() == definitionAbsolute;
}
definitionSharedLibrary ///< Only in shared libraries to model export.
};
+ /// The scope in which this atom is acessible to other atoms.
+ enum Scope {
+ scopeTranslationUnit, ///< Accessible only to atoms in the same translation
+ /// unit (e.g. a C static).
+ scopeLinkageUnit, ///< Accessible to atoms being linked but not visible
+ /// to runtime loader (e.g. visibility=hidden).
+ scopeGlobal ///< Accessible to all atoms and visible to runtime
+ /// loader (e.g. visibility=default).
+ };
+
+
/// file - returns the File that produced/owns this Atom
virtual const class File& file() const = 0;
///
class DefinedAtom : public Atom {
public:
- /// The scope in which this atom is acessible to other atoms.
- enum Scope {
- scopeTranslationUnit, ///< Accessible only to atoms in the same translation
- /// unit (e.g. a C static).
- scopeLinkageUnit, ///< Accessible to atoms being linked but not visible
- /// to runtime loader (e.g. visibility=hidden).
- scopeGlobal ///< Accessible to all atoms and visible to runtime
- /// loader (e.g. visibility=default).
- };
-
enum Interposable {
interposeNo, // linker can directly bind uses of this atom
interposeYes, // linker must indirect (through GOT) uses
// to consume an ELF file and produces atoms out of it.
//
//===----------------------------------------------------------------------===//
-
#include "lld/ReaderWriter/ReaderELF.h"
#include "lld/Core/File.h"
#include "lld/Core/Reference.h"
/// particular address. This atom has no content its address will be used by
/// the writer to fixup references that point to it.
template<llvm::support::endianness target_endianness, bool is64Bits>
-class ELFAbsoluteAtom final: public AbsoluteAtom {
+class ELFAbsoluteAtom final : public AbsoluteAtom {
+
+ typedef llvm::object::Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
public:
ELFAbsoluteAtom(const File &file,
llvm::StringRef name,
+ const Elf_Sym *symbol,
uint64_t value)
: _owningFile(file)
, _name(name)
+ , _symbol(symbol)
, _value(value)
{}
return _owningFile;
}
+ virtual Scope scope() const {
+ if (_symbol->st_other == llvm::ELF::STV_HIDDEN)
+ return scopeLinkageUnit;
+ if (_symbol->getBinding() == llvm::ELF::STB_LOCAL)
+ return scopeTranslationUnit;
+ else
+ return scopeGlobal;
+ }
+
virtual llvm::StringRef name() const {
return _name;
}
private:
const File &_owningFile;
llvm::StringRef _name;
+ const Elf_Sym *_symbol;
uint64_t _value;
};
ret = typeZeroFill;
break;
}
+
return ret;
}
|| _symbol->st_shndx == llvm::ELF::SHN_COMMON) {
return Alignment(llvm::Log2_64(_symbol->st_value));
}
-
return Alignment(llvm::Log2_64(_section->sh_addralign));
}
llvm::StringRef _sectionName;
const Elf_Sym *_symbol;
const Elf_Shdr *_section;
-
// _contentData will hold the bits that make up the atom.
llvm::ArrayRef<uint8_t> _contentData;
auto *newAtom = new (_readerStorage.Allocate
<ELFAbsoluteAtom<target_endianness, is64Bits> > ())
ELFAbsoluteAtom<target_endianness, is64Bits>
- (*this, symbolName, symbol->st_value);
+ (*this, symbolName, symbol, symbol->st_value);
_absoluteAtoms._atoms.push_back(newAtom);
_symbolToAtomMapping.insert(std::make_pair(symbol, newAtom));
b = ELF::STB_WEAK;
else
b = ELF::STB_GLOBAL;
- } else if (const AbsoluteAtom *aa = llvm::dyn_cast<const AbsoluteAtom>(a)){
+ } else if (const AbsoluteAtom *aa = llvm::dyn_cast<const AbsoluteAtom>(a)){
//FIXME: Absolute atoms need more properties to differentiate each other
// based on binding and type of symbol
- symbol->st_value = aa->value();
+ t = ELF::STT_OBJECT;
+
+ switch (aa->scope()) {
+ case AbsoluteAtom::scopeLinkageUnit:
+ symbol->st_other = ELF::STV_HIDDEN;
+ b = ELF::STB_LOCAL;
+ break;
+ case AbsoluteAtom::scopeTranslationUnit:
+ b = ELF::STB_LOCAL;
+ break;
+ case AbsoluteAtom::scopeGlobal:
+ b = ELF::STB_GLOBAL;
+ break;
+ }
+ symbol->st_value = aa->value();
} else {
- symbol->st_value = 0;
- t = ELF::STT_NOTYPE;
- b = ELF::STB_LOCAL;
+ symbol->st_value = 0;
+ t = ELF::STT_NOTYPE;
+ b = ELF::STB_LOCAL;
}
symbol->setBindingAndType(b, t);
enum NativeChunkSignatures {
NCS_DefinedAtomsV1 = 1,
NCS_AttributesArrayV1 = 2,
+ NCS_AbsoluteAttributesV1 = 12,
NCS_UndefinedAtomsV1 = 3,
NCS_SharedLibraryAtomsV1 = 4,
NCS_AbsoluteAtomsV1 = 5,
//
struct NativeAbsoluteAtomIvarsV1 {
uint32_t nameOffset;
+ uint32_t attributesOffset;
uint32_t reserved;
uint64_t value;
};
virtual const lld::File& file() const;
virtual StringRef name() const;
-
+ virtual Scope scope() const {
+ const NativeAtomAttributesV1& attr = absAttributes();
+ return (Scope)(attr.scope);
+ }
virtual uint64_t value() const {
return _ivarData->value;
}
private:
+ const NativeAtomAttributesV1& absAttributes() const;
const File *_file;
const NativeAbsoluteAtomIvarsV1 *_ivarData;
};
case NCS_AbsoluteAtomsV1:
ec = file->processAbsoluteAtomsV1(base, chunk);
break;
+ case NCS_AbsoluteAttributesV1:
+ ec = file->processAbsoluteAttributesV1(base, chunk);
+ break;
case NCS_ReferencesArrayV1:
ec = file->processReferencesV1(base, chunk);
break;
return make_error_code(native_reader_error::success);
}
+ // set up pointers to attributes array
+ error_code processAbsoluteAttributesV1(const uint8_t *base,
+ const NativeChunk *chunk) {
+ this->_absAttributes = base + chunk->fileOffset;
+ this->_absAbsoluteMaxOffset = chunk->fileSize;
+ DEBUG_WITH_TYPE("ReaderNative", llvm::dbgs()
+ << " chunk AbsoluteAttributesV1: "
+ << " count=" << chunk->elementCount
+ << " chunkSize=" << chunk->fileSize
+ << "\n");
+ return make_error_code(native_reader_error::success);
+ }
+
// instantiate array of UndefinedAtoms from v1 ivar data in file
error_code processUndefinedAtomsV1(const uint8_t *base,
const NativeChunk *chunk) {
return *reinterpret_cast<const NativeAtomAttributesV1*>(_attributes + off);
}
+ const NativeAtomAttributesV1& absAttribute(uint32_t off) const {
+ assert(off < _absAbsoluteMaxOffset);
+ return *reinterpret_cast<const NativeAtomAttributesV1*>(_absAttributes + off);
+ }
+
const uint8_t* content(uint32_t offset, uint32_t size) const {
const uint8_t* result = _contentStart + offset;
assert((result+size) <= _contentEnd);
AtomArray<UndefinedAtom> _undefinedAtoms;
AtomArray<SharedLibraryAtom> _sharedLibraryAtoms;
AtomArray<AbsoluteAtom> _absoluteAtoms;
+ const uint8_t* _absAttributes;
+ uint32_t _absAbsoluteMaxOffset;
const uint8_t* _attributes;
uint32_t _attributesMaxOffset;
IvarArray _references;
return _file->string(_ivarData->nameOffset);
}
+inline const NativeAtomAttributesV1& NativeAbsoluteAtomV1::absAttributes() const {
+ return _file->absAttribute(_ivarData->attributesOffset);
+}
inline const Atom* NativeReferenceV1::target() const {
return _file->target(_ivarData->targetIndex);
_absoluteAtomIvars.size()
* sizeof(NativeAbsoluteAtomIvarsV1));
}
+ if (!_absAttributes.empty()) {
+ assert( out.tell() == findChunk(NCS_AbsoluteAttributesV1).fileOffset );
+ out.write((char*)&_absAttributes[0],
+ _absAttributes.size()*sizeof(NativeAtomAttributesV1));
+ }
if (!_stringPool.empty()) {
assert( out.tell() == findChunk(NCS_Strings).fileOffset );
_absoluteAtomIndex[&atom] = _absoluteAtomIvars.size();
NativeAbsoluteAtomIvarsV1 ivar;
ivar.nameOffset = getNameOffset(atom);
+ ivar.attributesOffset = getAttributeOffset(atom);
ivar.reserved = 0;
ivar.value = atom.value();
_absoluteAtomIvars.push_back(ivar);
if ( hasDefines ) chunkCount += 2;
if ( hasUndefines ) ++chunkCount;
if ( hasSharedLibraries ) ++chunkCount;
- if ( hasAbsolutes ) ++chunkCount;
+ if ( hasAbsolutes ) chunkCount += 2;
if ( hasReferences ) ++chunkCount;
if ( hasTargetsTable ) ++chunkCount;
if ( hasAddendTable ) ++chunkCount;
// create chunk for shared library atom array
if ( hasAbsolutes ) {
- NativeChunk& chsl = chunks[nextIndex++];
- chsl.signature = NCS_AbsoluteAtomsV1;
- chsl.fileOffset = nextFileOffset;
- chsl.fileSize = _absoluteAtomIvars.size() *
+ NativeChunk& chabs = chunks[nextIndex++];
+ chabs.signature = NCS_AbsoluteAtomsV1;
+ chabs.fileOffset = nextFileOffset;
+ chabs.fileSize = _absoluteAtomIvars.size() *
sizeof(NativeAbsoluteAtomIvarsV1);
- chsl.elementCount = _absoluteAtomIvars.size();
- nextFileOffset = chsl.fileOffset + chsl.fileSize;
+ chabs.elementCount = _absoluteAtomIvars.size();
+ nextFileOffset = chabs.fileOffset + chabs.fileSize;
+
+ // create chunk for attributes
+ NativeChunk& cha = chunks[nextIndex++];
+ cha.signature = NCS_AbsoluteAttributesV1;
+ cha.fileOffset = nextFileOffset;
+ cha.fileSize = _absAttributes.size()*sizeof(NativeAtomAttributesV1);
+ cha.elementCount = _absAttributes.size();
+ nextFileOffset = cha.fileOffset + cha.fileSize;
}
// create chunk for symbol strings
return result;
}
+ uint32_t getAttributeOffset(const class AbsoluteAtom& atom) {
+ NativeAtomAttributesV1 attrs;
+ computeAbsoluteAttributes(atom, attrs);
+ for(unsigned int i=0; i < _absAttributes.size(); ++i) {
+ if ( !memcmp(&_absAttributes[i], &attrs, sizeof(NativeAtomAttributesV1)) ) {
+ // found that this set of attributes already used, so re-use
+ return i * sizeof(NativeAtomAttributesV1);
+ }
+ }
+ // append new attribute set to end
+ uint32_t result = _absAttributes.size() * sizeof(NativeAtomAttributesV1);
+ _absAttributes.push_back(attrs);
+ return result;
+ }
+
uint32_t sectionNameOffset(const class DefinedAtom& atom) {
// if section based on content, then no custom section name available
if ( atom.sectionChoice() == DefinedAtom::sectionBasedOnContent )
attrs.alias = atom.isAlias();
}
+ void computeAbsoluteAttributes(const class AbsoluteAtom& atom,
+ NativeAtomAttributesV1& attrs) {
+ attrs.scope = atom.scope();
+ }
+
// add references for this atom in a contiguous block in NCS_ReferencesArrayV1
uint32_t getReferencesIndex(const DefinedAtom& atom, unsigned& count) {
count = 0;
std::vector<uint8_t> _contentPool;
std::vector<NativeDefinedAtomIvarsV1> _definedAtomIvars;
std::vector<NativeAtomAttributesV1> _attributes;
+ std::vector<NativeAtomAttributesV1> _absAttributes;
std::vector<NativeUndefinedAtomIvarsV1> _undefinedAtomIvars;
std::vector<NativeSharedLibraryAtomIvarsV1> _sharedLibraryAtomIvars;
std::vector<NativeAbsoluteAtomIvarsV1> _absoluteAtomIvars;
return OwningFile;
}
+ virtual Scope scope() const {
+ return scopeGlobal;
+ }
+
virtual llvm::StringRef name() const {
return Name;
}
return _file;
}
+ virtual Scope scope() const {
+ return scopeGlobal;
+ }
+
virtual StringRef name() const {
return _name;
}
<< KeyValues::definition(atom.definition())
<< "\n";
+ if ( atom.scope() != KeyValues::scopeDefault ) {
+ out << " "
+ << "scope:"
+ << spacePadding(strlen("scope"))
+ << KeyValues::scope(atom.scope())
+ << "\n";
+ }
+
out << " "
<< "value:"
<< spacePadding(strlen("value"))
};
static const ScopeMapping scopeMappings[] = {
- { "global", DefinedAtom::scopeGlobal },
- { "hidden", DefinedAtom::scopeLinkageUnit },
- { "static", DefinedAtom::scopeTranslationUnit },
- { nullptr, DefinedAtom::scopeGlobal }
+ { "global", Atom::scopeGlobal },
+ { "hidden", Atom::scopeLinkageUnit },
+ { "static", Atom::scopeTranslationUnit },
+ { nullptr, Atom::scopeGlobal }
};
bool KeyValues::scope(StringRef s, DefinedAtom::Scope &out)
return true;
}
-const char* KeyValues::scope(DefinedAtom::Scope s) {
+const char* KeyValues::scope(Atom::Scope s) {
for (const ScopeMapping* p = scopeMappings; p->string != nullptr; ++p) {
if ( p->value == s )
return p->string;
-
struct ContentTypeMapping {
const char* string;
DefinedAtom::ContentType value;
static const char* const scopeKeyword;
static const DefinedAtom::Scope scopeDefault;
static bool scope(StringRef, DefinedAtom::Scope&);
- static const char* scope(DefinedAtom::Scope);
+ static const char* scope(Atom::Scope);
static const char* const contentTypeKeyword;
static const DefinedAtom::ContentType contentTypeDefault;
--- /dev/null
+#
+# Source File:
+# .local absGlobalSymbol
+# .set absLocalSymbol,0xC0000
+# .type absLocalSymbol, @object
+# .globl absGlobalSymbol
+# .set absGlobalSymbol,0xD0000
+# .type absGlobalSymbol, @object
+
+# built using: "gcc -m32"
+#
+RUN: lld-core -reader ELF %p/Inputs/abs-test.i386 | FileCheck -check-prefix=YAML %s
+
+YAML: - name: absLocalSymbol
+YAML: definition: absolute
+YAML: value: 0xc0000
+
+YAML: - name: absGlobalSymbol
+YAML: definition: absolute
+YAML: scope: global
+YAML: value: 0xd0000