From 990ec2b223896e8e839f54f0946b19730692087e Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Mon, 28 Jan 2013 04:15:44 +0000 Subject: [PATCH] [ELF] Use entry point from LinkerOptions. Patch by Ahmed Bougacha! llvm-svn: 173655 --- lld/include/lld/ReaderWriter/ELFTargetInfo.h | 1 + lld/lib/Driver/Drivers.cpp | 3 - lld/lib/ReaderWriter/ELF/ELFTargetInfo.cpp | 7 +++ lld/lib/ReaderWriter/ELF/WriterELF.cpp | 4 +- .../ReaderWriter/MachO/MachOTargetInfo.cpp | 2 - lld/test/elf/entry.objtxt | 57 +++++++++++++++++++ lld/tools/lld-core/lld-core.cpp | 6 ++ 7 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 lld/test/elf/entry.objtxt diff --git a/lld/include/lld/ReaderWriter/ELFTargetInfo.h b/lld/include/lld/ReaderWriter/ELFTargetInfo.h index 237bd4f221e9..7914083f83c0 100644 --- a/lld/include/lld/ReaderWriter/ELFTargetInfo.h +++ b/lld/include/lld/ReaderWriter/ELFTargetInfo.h @@ -34,6 +34,7 @@ public: uint16_t getOutputType() const; uint16_t getOutputMachine() const; + virtual StringRef getEntry() const; virtual uint64_t getBaseAddress() const { return _options._baseAddress; } static std::unique_ptr create(const LinkerOptions &lo); diff --git a/lld/lib/Driver/Drivers.cpp b/lld/lib/Driver/Drivers.cpp index d6c29ca175e9..00085e685f93 100644 --- a/lld/lib/Driver/Drivers.cpp +++ b/lld/lib/Driver/Drivers.cpp @@ -124,9 +124,6 @@ public: if (llvm::opt::Arg *A = _inputArgs->getLastArg(ld::OPT_entry)) newArgs->AddJoinedArg(A, _core.getOption(core::OPT_entry), A->getValue()); - else - newArgs->AddJoinedArg(nullptr, _core.getOption(core::OPT_entry), - "_start"); if (llvm::opt::Arg *A = _inputArgs->getLastArg(ld::OPT_output)) newArgs->AddJoinedArg(A, _core.getOption(core::OPT_output), diff --git a/lld/lib/ReaderWriter/ELF/ELFTargetInfo.cpp b/lld/lib/ReaderWriter/ELF/ELFTargetInfo.cpp index cb398ebccc82..ea0fd114c4aa 100644 --- a/lld/lib/ReaderWriter/ELF/ELFTargetInfo.cpp +++ b/lld/lib/ReaderWriter/ELF/ELFTargetInfo.cpp @@ -68,4 +68,11 @@ std::unique_ptr ELFTargetInfo::create(const LinkerOptions &lo) { return std::unique_ptr(); } } + +StringRef ELFTargetInfo::getEntry() const { + if (!_options._entrySymbol.empty()) + return _options._entrySymbol; + return "_start"; +} + } // end namespace lld diff --git a/lld/lib/ReaderWriter/ELF/WriterELF.cpp b/lld/lib/ReaderWriter/ELF/WriterELF.cpp index f8d23c5fb26d..ecefa19b8b8f 100644 --- a/lld/lib/ReaderWriter/ELF/WriterELF.cpp +++ b/lld/lib/ReaderWriter/ELF/WriterELF.cpp @@ -149,7 +149,7 @@ void ELFExecutableWriter::assignSectionsWithNoSegments() { /// absolute symbols template void ELFExecutableWriter::addDefaultAtoms() { - _runtimeFile.addUndefinedAtom("_start"); + _runtimeFile.addUndefinedAtom(_targetInfo.getEntry()); _runtimeFile.addAbsoluteAtom("__bss_start"); _runtimeFile.addAbsoluteAtom("__bss_end"); _runtimeFile.addAbsoluteAtom("_end"); @@ -268,7 +268,7 @@ ELFExecutableWriter::writeFile(const lld::File &file, StringRef path) { _elfHeader->e_shnum(_shdrtab->numHeaders()); _elfHeader->e_shstrndx(_shstrtab->ordinal()); uint64_t virtualAddr = 0; - _layout->findAtomAddrByName("_start", virtualAddr); + _layout->findAtomAddrByName(_targetInfo.getEntry(), virtualAddr); _elfHeader->e_entry(virtualAddr); // HACK: We have to write out the header and program header here even though diff --git a/lld/lib/ReaderWriter/MachO/MachOTargetInfo.cpp b/lld/lib/ReaderWriter/MachO/MachOTargetInfo.cpp index fd5728d864d9..621b6476af94 100644 --- a/lld/lib/ReaderWriter/MachO/MachOTargetInfo.cpp +++ b/lld/lib/ReaderWriter/MachO/MachOTargetInfo.cpp @@ -70,8 +70,6 @@ public: virtual uint64_t getPageZeroSize() const { return getPageSize(); } virtual StringRef getEntry() const { - if (_options._outputKind != OutputKind::Executable) - return ""; if (!_options._entrySymbol.empty()) return _options._entrySymbol; return "_main"; diff --git a/lld/test/elf/entry.objtxt b/lld/test/elf/entry.objtxt new file mode 100644 index 000000000000..63492562306f --- /dev/null +++ b/lld/test/elf/entry.objtxt @@ -0,0 +1,57 @@ +# Tests entry point handling +# +# Test generated using the source file below: +# +# int main() +# { +# return 0; +# } +# + +# RUN: lld-core -reader YAML %s -writer ELF --entry=_entrypoint -o %t1 +# RUN: llvm-nm -n %t1 | FileCheck %s +# +# CHECK: 000000a0 T main +# CHECK: 00001000 A _end +# CHECK: 00001000 A end +# CHECK: w _entrypoint + +defined-atoms: + - name: .text + alignment: 2^4 + section-choice: custom-required + section-name: .text + - name: main + scope: global + content: [ B8, 00, 00, 00, 00, C7, 44, 24, FC, 00, 00, 00, + 00, C3 ] + alignment: 2^4 + section-choice: custom-required + section-name: .text + - name: .data + type: data + alignment: 2^2 + section-choice: custom-required + section-name: .data + - name: .bss + type: zero-fill + alignment: 2^2 + section-choice: custom-required + section-name: .bss + - name: .note.GNU-stack + section-choice: custom-required + section-name: .note.GNU-stack + permissions: r-- + - name: .eh_frame + content: [ 14, 00, 00, 00, 00, 00, 00, 00, 01, 7A, 52, 00, + 01, 78, 10, 01, 1B, 0C, 07, 08, 90, 01, 00, 00, + 14, 00, 00, 00, 1C, 00, 00, 00, 00, 00, 00, 00, + 0E, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 ] + alignment: 2^3 + section-choice: custom-required + section-name: .eh_frame + permissions: r-- + references: + - kind: call32 + offset: 32 + target: .text diff --git a/lld/tools/lld-core/lld-core.cpp b/lld/tools/lld-core/lld-core.cpp index 569aab777fc0..bec87e08df1b 100644 --- a/lld/tools/lld-core/lld-core.cpp +++ b/lld/tools/lld-core/lld-core.cpp @@ -92,6 +92,11 @@ llvm::cl::opt cmdLineGlobalsNotDeadStrip("keep-globals", llvm::cl::desc("All global symbols are roots for dead-strip")); +llvm::cl::opt +cmdLineEntryPoint("entry", + llvm::cl::desc("Specify entry point symbol"), + llvm::cl::value_desc("symbol")); + enum WriteChoice { writeYAML, writeMachO, writePECOFF, writeELF @@ -221,6 +226,7 @@ int main(int argc, char *argv[]) { lo._globalsAreDeadStripRoots = cmdLineGlobalsNotDeadStrip; lo._forceLoadArchives = cmdLineForceLoad; lo._outputKind = OutputKind::Executable; + lo._entrySymbol = cmdLineEntryPoint; switch (archSelected) { case i386: