2015-09-22 00:01:39 +00:00
|
|
|
//===- InputSection.cpp ---------------------------------------------------===//
|
2015-07-24 21:03:07 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-07-24 21:03:07 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-09-22 00:01:39 +00:00
|
|
|
#include "InputSection.h"
|
2015-09-25 19:24:57 +00:00
|
|
|
#include "Config.h"
|
2016-05-24 02:55:45 +00:00
|
|
|
#include "EhFrame.h"
|
2015-08-27 23:15:56 +00:00
|
|
|
#include "InputFiles.h"
|
2016-07-12 08:50:42 +00:00
|
|
|
#include "LinkerScript.h"
|
2015-09-21 22:01:00 +00:00
|
|
|
#include "OutputSections.h"
|
2016-11-23 10:07:46 +00:00
|
|
|
#include "Relocations.h"
|
2018-07-17 23:16:02 +00:00
|
|
|
#include "SymbolTable.h"
|
2017-12-09 16:56:18 +00:00
|
|
|
#include "Symbols.h"
|
2016-11-10 09:48:29 +00:00
|
|
|
#include "SyntheticSections.h"
|
2015-09-22 18:19:46 +00:00
|
|
|
#include "Target.h"
|
2016-07-08 16:10:27 +00:00
|
|
|
#include "Thunks.h"
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-25 22:28:38 +00:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-11-28 20:39:17 +00:00
|
|
|
#include "lld/Common/Memory.h"
|
2017-06-08 20:16:21 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2016-06-24 11:18:44 +00:00
|
|
|
#include "llvm/Support/Compression.h"
|
2016-02-25 21:33:56 +00:00
|
|
|
#include "llvm/Support/Endian.h"
|
2017-05-24 18:31:48 +00:00
|
|
|
#include "llvm/Support/Threading.h"
|
2017-09-30 21:28:49 +00:00
|
|
|
#include "llvm/Support/xxhash.h"
|
2018-07-17 23:16:02 +00:00
|
|
|
#include <algorithm>
|
Parallelize uncompress() and splitIntoPieces().
Uncompressing section contents and spliting mergeable section contents
into smaller chunks are heavy tasks. They scan entire section contents
and do CPU-intensive tasks such as uncompressing zlib-compressed data
or computing a hash value for each section piece.
Luckily, these tasks are independent to each other, so we can do that
in parallel_for_each. The number of input sections is large (as opposed
to the number of output sections), so there's a large parallelism here.
Actually the current design to call uncompress() and splitIntoPieces()
in batch was chosen with doing this in mind. Basically what we need to
do here is to replace `for` with `parallel_for_each`.
It seems this patch improves latency significantly if linked programs
contain debug info (which in turn contain lots of mergeable strings.)
For example, the latency to link Clang (debug build) improved by 20% on
my machine as shown below. Note that ld.gold took 19.2 seconds to do
the same thing.
Before:
30801.782712 task-clock (msec) # 3.652 CPUs utilized ( +- 2.59% )
104,084 context-switches # 0.003 M/sec ( +- 1.02% )
5,063 cpu-migrations # 0.164 K/sec ( +- 13.66% )
2,528,130 page-faults # 0.082 M/sec ( +- 0.47% )
85,317,809,130 cycles # 2.770 GHz ( +- 2.62% )
67,352,463,373 stalled-cycles-frontend # 78.94% frontend cycles idle ( +- 3.06% )
<not supported> stalled-cycles-backend
44,295,945,493 instructions # 0.52 insns per cycle
# 1.52 stalled cycles per insn ( +- 0.44% )
8,572,384,877 branches # 278.308 M/sec ( +- 0.66% )
141,806,726 branch-misses # 1.65% of all branches ( +- 0.13% )
8.433424003 seconds time elapsed ( +- 1.20% )
After:
35523.764575 task-clock (msec) # 5.265 CPUs utilized ( +- 2.67% )
159,107 context-switches # 0.004 M/sec ( +- 0.48% )
8,123 cpu-migrations # 0.229 K/sec ( +- 23.34% )
2,372,483 page-faults # 0.067 M/sec ( +- 0.36% )
98,395,342,152 cycles # 2.770 GHz ( +- 2.62% )
79,294,670,125 stalled-cycles-frontend # 80.59% frontend cycles idle ( +- 3.03% )
<not supported> stalled-cycles-backend
46,274,151,813 instructions # 0.47 insns per cycle
# 1.71 stalled cycles per insn ( +- 0.47% )
8,987,621,670 branches # 253.003 M/sec ( +- 0.60% )
148,900,624 branch-misses # 1.66% of all branches ( +- 0.27% )
6.747548004 seconds time elapsed ( +- 0.40% )
llvm-svn: 287946
2016-11-25 20:05:08 +00:00
|
|
|
#include <mutex>
|
2018-07-17 23:16:02 +00:00
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
2016-02-25 21:33:56 +00:00
|
|
|
|
2015-07-24 21:03:07 +00:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::ELF;
|
2015-09-21 22:01:00 +00:00
|
|
|
using namespace llvm::object;
|
2016-10-12 22:36:31 +00:00
|
|
|
using namespace llvm::support;
|
2016-02-25 21:33:56 +00:00
|
|
|
using namespace llvm::support::endian;
|
2017-03-30 19:13:47 +00:00
|
|
|
using namespace llvm::sys;
|
2015-07-24 21:03:07 +00:00
|
|
|
|
2019-10-07 08:31:18 +00:00
|
|
|
namespace lld {
|
2016-11-23 18:07:33 +00:00
|
|
|
// Returns a string to construct an error message.
|
2019-10-07 08:31:18 +00:00
|
|
|
std::string toString(const elf::InputSectionBase *sec) {
|
2017-04-28 20:00:09 +00:00
|
|
|
return (toString(sec->file) + ":(" + sec->name + ")").str();
|
2016-11-23 18:07:33 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 08:31:18 +00:00
|
|
|
namespace elf {
|
|
|
|
std::vector<InputSectionBase *> inputSections;
|
|
|
|
|
2016-09-12 13:13:53 +00:00
|
|
|
template <class ELFT>
|
2017-12-21 02:03:39 +00:00
|
|
|
static ArrayRef<uint8_t> getSectionContents(ObjFile<ELFT> &file,
|
|
|
|
const typename ELFT::Shdr &hdr) {
|
|
|
|
if (hdr.sh_type == SHT_NOBITS)
|
|
|
|
return makeArrayRef<uint8_t>(nullptr, hdr.sh_size);
|
|
|
|
return check(file.getObj().getSectionContents(&hdr));
|
2016-09-12 13:13:53 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 02:28:28 +00:00
|
|
|
InputSectionBase::InputSectionBase(InputFile *file, uint64_t flags,
|
|
|
|
uint32_t type, uint64_t entsize,
|
|
|
|
uint32_t link, uint32_t info,
|
2017-03-08 19:35:29 +00:00
|
|
|
uint32_t alignment, ArrayRef<uint8_t> data,
|
2017-02-23 02:28:28 +00:00
|
|
|
StringRef name, Kind sectionKind)
|
2017-03-08 22:36:28 +00:00
|
|
|
: SectionBase(sectionKind, name, flags, entsize, alignment, type, info,
|
|
|
|
link),
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
file(file), rawData(data) {
|
2017-12-21 01:21:59 +00:00
|
|
|
// In order to reduce memory allocation, we assume that mergeable
|
|
|
|
// sections are smaller than 4 GiB, which is not an unreasonable
|
|
|
|
// assumption as of 2017.
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
if (sectionKind == SectionBase::Merge && rawData.size() > UINT32_MAX)
|
2017-12-21 01:21:59 +00:00
|
|
|
error(toString(this) + ": section too large");
|
|
|
|
|
2016-11-10 14:53:24 +00:00
|
|
|
numRelocations = 0;
|
|
|
|
areRelocsRela = false;
|
|
|
|
|
2016-02-24 00:38:18 +00:00
|
|
|
// The ELF spec states that a value of 0 means the section has
|
2019-10-29 01:41:38 +00:00
|
|
|
// no alignment constraints.
|
2019-04-26 04:07:58 +00:00
|
|
|
uint32_t v = std::max<uint32_t>(alignment, 1);
|
2016-10-07 12:27:45 +00:00
|
|
|
if (!isPowerOf2_64(v))
|
2019-02-27 10:28:23 +00:00
|
|
|
fatal(toString(this) + ": sh_addralign is not a power of 2");
|
2017-03-07 15:11:21 +00:00
|
|
|
this->alignment = v;
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
|
|
|
|
// In ELF, each section can be compressed by zlib, and if compressed,
|
|
|
|
// section name may be mangled by appending "z" (e.g. ".zdebug_info").
|
|
|
|
// If that's the case, demangle section name so that we can handle a
|
|
|
|
// section as if it weren't compressed.
|
|
|
|
if ((flags & SHF_COMPRESSED) || name.startswith(".zdebug")) {
|
|
|
|
if (!zlib::isAvailable())
|
|
|
|
error(toString(file) + ": contains a compressed section, " +
|
|
|
|
"but zlib is not available");
|
|
|
|
parseCompressedHeader();
|
|
|
|
}
|
2016-02-24 00:23:15 +00:00
|
|
|
}
|
2015-10-19 21:00:02 +00:00
|
|
|
|
2017-06-10 00:38:55 +00:00
|
|
|
// Drop SHF_GROUP bit unless we are producing a re-linkable object file.
|
|
|
|
// SHF_GROUP is a marker that a section belongs to some comdat group.
|
|
|
|
// That flag doesn't make sense in an executable.
|
|
|
|
static uint64_t getFlags(uint64_t flags) {
|
|
|
|
flags &= ~(uint64_t)SHF_INFO_LINK;
|
|
|
|
if (!config->relocatable)
|
|
|
|
flags &= ~(uint64_t)SHF_GROUP;
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2017-03-23 01:00:41 +00:00
|
|
|
// GNU assembler 2.24 and LLVM 4.0.0's MC (the newest release as of
|
|
|
|
// March 2017) fail to infer section types for sections starting with
|
|
|
|
// ".init_array." or ".fini_array.". They set SHT_PROGBITS instead of
|
|
|
|
// SHF_INIT_ARRAY. As a result, the following assembler directive
|
|
|
|
// creates ".init_array.100" with SHT_PROGBITS, for example.
|
|
|
|
//
|
|
|
|
// .section .init_array.100, "aw"
|
|
|
|
//
|
|
|
|
// This function forces SHT_{INIT,FINI}_ARRAY so that we can handle
|
|
|
|
// incorrect inputs as if they were correct from the beginning.
|
|
|
|
static uint64_t getType(uint64_t type, StringRef name) {
|
|
|
|
if (type == SHT_PROGBITS && name.startswith(".init_array."))
|
|
|
|
return SHT_INIT_ARRAY;
|
|
|
|
if (type == SHT_PROGBITS && name.startswith(".fini_array."))
|
|
|
|
return SHT_FINI_ARRAY;
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2016-10-26 00:54:03 +00:00
|
|
|
template <class ELFT>
|
2017-12-21 02:03:39 +00:00
|
|
|
InputSectionBase::InputSectionBase(ObjFile<ELFT> &file,
|
|
|
|
const typename ELFT::Shdr &hdr,
|
2017-02-23 02:28:28 +00:00
|
|
|
StringRef name, Kind sectionKind)
|
2017-12-21 02:03:39 +00:00
|
|
|
: InputSectionBase(&file, getFlags(hdr.sh_flags),
|
|
|
|
getType(hdr.sh_type, name), hdr.sh_entsize, hdr.sh_link,
|
|
|
|
hdr.sh_info, hdr.sh_addralign,
|
2017-03-23 01:00:41 +00:00
|
|
|
getSectionContents(file, hdr), name, sectionKind) {
|
2017-03-08 19:35:29 +00:00
|
|
|
// We reject object files having insanely large alignments even though
|
|
|
|
// they are allowed by the spec. I think 4GB is a reasonable limitation.
|
|
|
|
// We might want to relax this in the future.
|
2017-12-21 02:03:39 +00:00
|
|
|
if (hdr.sh_addralign > UINT32_MAX)
|
|
|
|
fatal(toString(&file) + ": section sh_addralign is too large");
|
2016-11-01 09:17:50 +00:00
|
|
|
}
|
2016-10-26 00:54:03 +00:00
|
|
|
|
2017-03-08 15:44:30 +00:00
|
|
|
size_t InputSectionBase::getSize() const {
|
2017-02-27 02:56:02 +00:00
|
|
|
if (auto *s = dyn_cast<SyntheticSection>(this))
|
2016-11-10 09:48:29 +00:00
|
|
|
return s->getSize();
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
if (uncompressedSize >= 0)
|
|
|
|
return uncompressedSize;
|
|
|
|
return rawData.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputSectionBase::uncompress() const {
|
|
|
|
size_t size = uncompressedSize;
|
2019-03-12 20:32:30 +00:00
|
|
|
char *uncompressedBuf;
|
|
|
|
{
|
|
|
|
static std::mutex mu;
|
|
|
|
std::lock_guard<std::mutex> lock(mu);
|
|
|
|
uncompressedBuf = bAlloc.Allocate<char>(size);
|
|
|
|
}
|
2016-11-10 09:48:29 +00:00
|
|
|
|
2019-03-12 20:32:30 +00:00
|
|
|
if (Error e = zlib::uncompress(toStringRef(rawData), uncompressedBuf, size))
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
fatal(toString(this) +
|
|
|
|
": uncompress failed: " + llvm::toString(std::move(e)));
|
2019-03-12 20:32:30 +00:00
|
|
|
rawData = makeArrayRef((uint8_t *)uncompressedBuf, size);
|
|
|
|
uncompressedSize = -1;
|
2016-11-08 14:47:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 15:57:17 +00:00
|
|
|
uint64_t InputSectionBase::getOffsetInFile() const {
|
2017-03-08 14:12:52 +00:00
|
|
|
const uint8_t *fileStart = (const uint8_t *)file->mb.getBufferStart();
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
const uint8_t *secStart = data().begin();
|
2017-03-08 14:12:52 +00:00
|
|
|
return secStart - fileStart;
|
|
|
|
}
|
|
|
|
|
2017-03-08 22:36:28 +00:00
|
|
|
uint64_t SectionBase::getOffset(uint64_t offset) const {
|
2016-09-08 12:33:41 +00:00
|
|
|
switch (kind()) {
|
2017-03-08 22:36:28 +00:00
|
|
|
case Output: {
|
|
|
|
auto *os = cast<OutputSection>(this);
|
|
|
|
// For output sections we treat offset -1 as the end of the section.
|
|
|
|
return offset == uint64_t(-1) ? os->size : offset;
|
|
|
|
}
|
2015-11-11 16:50:37 +00:00
|
|
|
case Regular:
|
2018-04-19 16:54:30 +00:00
|
|
|
case Synthetic:
|
2018-04-19 18:00:46 +00:00
|
|
|
return cast<InputSection>(this)->getOffset(offset);
|
2015-11-11 19:54:14 +00:00
|
|
|
case EHFrame:
|
2016-07-20 20:19:58 +00:00
|
|
|
// The file crtbeginT.o has relocations pointing to the start of an empty
|
|
|
|
// .eh_frame that is known to be the first in the link. It does that to
|
|
|
|
// identify the start of the output .eh_frame.
|
|
|
|
return offset;
|
2015-11-11 16:50:37 +00:00
|
|
|
case Merge:
|
2017-03-06 20:23:56 +00:00
|
|
|
const MergeInputSection *ms = cast<MergeInputSection>(this);
|
2017-05-31 20:17:44 +00:00
|
|
|
if (InputSection *isec = ms->getParent())
|
2018-04-19 18:00:46 +00:00
|
|
|
return isec->getOffset(ms->getParentOffset(offset));
|
2018-04-19 16:05:07 +00:00
|
|
|
return ms->getParentOffset(offset);
|
2015-11-11 16:50:37 +00:00
|
|
|
}
|
2016-03-12 08:31:34 +00:00
|
|
|
llvm_unreachable("invalid section kind");
|
2015-11-11 16:50:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 00:35:11 +00:00
|
|
|
uint64_t SectionBase::getVA(uint64_t offset) const {
|
|
|
|
const OutputSection *out = getOutputSection();
|
|
|
|
return (out ? out->addr : 0) + getOffset(offset);
|
|
|
|
}
|
|
|
|
|
2017-03-08 22:36:28 +00:00
|
|
|
OutputSection *SectionBase::getOutputSection() {
|
2017-05-31 20:17:44 +00:00
|
|
|
InputSection *sec;
|
2017-03-08 22:36:28 +00:00
|
|
|
if (auto *isec = dyn_cast<InputSection>(this))
|
2018-03-23 17:19:18 +00:00
|
|
|
sec = isec;
|
2017-05-31 20:17:44 +00:00
|
|
|
else if (auto *ms = dyn_cast<MergeInputSection>(this))
|
|
|
|
sec = ms->getParent();
|
|
|
|
else if (auto *eh = dyn_cast<EhInputSection>(this))
|
|
|
|
sec = eh->getParent();
|
|
|
|
else
|
|
|
|
return cast<OutputSection>(this);
|
2018-04-19 17:26:50 +00:00
|
|
|
return sec ? sec->getParent() : nullptr;
|
2017-02-03 13:06:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:50:45 +00:00
|
|
|
// When a section is compressed, `rawData` consists with a header followed
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
// by zlib-compressed data. This function parses a header to initialize
|
2019-07-16 05:50:45 +00:00
|
|
|
// `uncompressedSize` member and remove the header from `rawData`.
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
void InputSectionBase::parseCompressedHeader() {
|
2019-04-01 00:11:24 +00:00
|
|
|
using Chdr64 = typename ELF64LE::Chdr;
|
|
|
|
using Chdr32 = typename ELF32LE::Chdr;
|
2018-10-09 21:41:53 +00:00
|
|
|
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
// Old-style header
|
|
|
|
if (name.startswith(".zdebug")) {
|
|
|
|
if (!toStringRef(rawData).startswith("ZLIB")) {
|
|
|
|
error(toString(this) + ": corrupted compressed section header");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rawData = rawData.slice(4);
|
2017-10-04 00:19:41 +00:00
|
|
|
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
if (rawData.size() < 8) {
|
|
|
|
error(toString(this) + ": corrupted compressed section header");
|
|
|
|
return;
|
|
|
|
}
|
2017-01-12 10:53:31 +00:00
|
|
|
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
uncompressedSize = read64be(rawData.data());
|
|
|
|
rawData = rawData.slice(8);
|
2017-08-17 00:27:55 +00:00
|
|
|
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
// Restore the original section name.
|
|
|
|
// (e.g. ".zdebug_info" -> ".debug_info")
|
|
|
|
name = saver.save("." + name.substr(2));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(flags & SHF_COMPRESSED);
|
2017-12-19 16:29:02 +00:00
|
|
|
flags &= ~(uint64_t)SHF_COMPRESSED;
|
2018-02-12 22:25:45 +00:00
|
|
|
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
// New-style 64-bit header
|
|
|
|
if (config->is64) {
|
2018-10-09 21:41:53 +00:00
|
|
|
if (rawData.size() < sizeof(Chdr64)) {
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
error(toString(this) + ": corrupted compressed section");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-09 21:41:53 +00:00
|
|
|
auto *hdr = reinterpret_cast<const Chdr64 *>(rawData.data());
|
|
|
|
if (hdr->ch_type != ELFCOMPRESS_ZLIB) {
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
error(toString(this) + ": unsupported compression type");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-09 21:41:53 +00:00
|
|
|
uncompressedSize = hdr->ch_size;
|
2019-04-26 04:07:58 +00:00
|
|
|
alignment = std::max<uint32_t>(hdr->ch_addralign, 1);
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
rawData = rawData.slice(sizeof(*hdr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// New-style 32-bit header
|
2018-10-09 21:41:53 +00:00
|
|
|
if (rawData.size() < sizeof(Chdr32)) {
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
error(toString(this) + ": corrupted compressed section");
|
|
|
|
return;
|
2018-02-12 22:25:45 +00:00
|
|
|
}
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
|
2018-10-09 21:41:53 +00:00
|
|
|
auto *hdr = reinterpret_cast<const Chdr32 *>(rawData.data());
|
|
|
|
if (hdr->ch_type != ELFCOMPRESS_ZLIB) {
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
error(toString(this) + ": unsupported compression type");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-09 21:41:53 +00:00
|
|
|
uncompressedSize = hdr->ch_size;
|
2019-04-26 04:07:58 +00:00
|
|
|
alignment = std::max<uint32_t>(hdr->ch_addralign, 1);
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
rawData = rawData.slice(sizeof(*hdr));
|
2016-06-24 11:18:44 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 19:09:52 +00:00
|
|
|
InputSection *InputSectionBase::getLinkOrderDep() const {
|
2018-03-08 15:06:58 +00:00
|
|
|
assert(link);
|
|
|
|
assert(flags & SHF_LINK_ORDER);
|
|
|
|
return cast<InputSection>(file->getSections()[link]);
|
2016-10-10 09:39:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 23:16:02 +00:00
|
|
|
// Find a function symbol that encloses a given location.
|
|
|
|
template <class ELFT>
|
|
|
|
Defined *InputSectionBase::getEnclosingFunction(uint64_t offset) {
|
|
|
|
for (Symbol *b : file->getSymbols())
|
|
|
|
if (Defined *d = dyn_cast<Defined>(b))
|
2018-08-01 08:11:54 +00:00
|
|
|
if (d->section == this && d->type == STT_FUNC && d->value <= offset &&
|
|
|
|
offset < d->value + d->size)
|
2018-07-17 23:16:02 +00:00
|
|
|
return d;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-25 18:51:53 +00:00
|
|
|
// Returns a source location string. Used to construct an error message.
|
|
|
|
template <class ELFT>
|
2017-02-23 02:28:28 +00:00
|
|
|
std::string InputSectionBase::getLocation(uint64_t offset) {
|
2019-01-10 15:08:06 +00:00
|
|
|
std::string secAndOffset = (name + "+0x" + utohexstr(offset)).str();
|
|
|
|
|
2017-03-14 08:33:45 +00:00
|
|
|
// We don't have file for synthetic sections.
|
|
|
|
if (getFile<ELFT>() == nullptr)
|
2019-01-10 15:08:06 +00:00
|
|
|
return (config->outputFile + ":(" + secAndOffset + ")")
|
2017-03-14 08:33:45 +00:00
|
|
|
.str();
|
|
|
|
|
2016-11-25 18:51:53 +00:00
|
|
|
// First check if we can get desired values from debugging information.
|
2018-07-16 15:29:35 +00:00
|
|
|
if (Optional<DILineInfo> info = getFile<ELFT>()->getDILineInfo(this, offset))
|
2019-01-10 15:08:06 +00:00
|
|
|
return info->FileName + ":" + std::to_string(info->Line) + ":(" +
|
|
|
|
secAndOffset + ")";
|
2016-11-25 18:51:53 +00:00
|
|
|
|
2019-07-16 05:50:45 +00:00
|
|
|
// File->sourceFile contains STT_FILE symbol that contains a
|
2016-11-25 18:51:53 +00:00
|
|
|
// source file name. If it's missing, we use an object file name.
|
2017-02-23 02:28:28 +00:00
|
|
|
std::string srcFile = getFile<ELFT>()->sourceFile;
|
2016-11-25 18:51:53 +00:00
|
|
|
if (srcFile.empty())
|
|
|
|
srcFile = toString(file);
|
|
|
|
|
2018-07-17 23:16:02 +00:00
|
|
|
if (Defined *d = getEnclosingFunction<ELFT>(offset))
|
2019-01-10 15:08:06 +00:00
|
|
|
return srcFile + ":(function " + toString(*d) + ": " + secAndOffset + ")";
|
2016-11-25 18:51:53 +00:00
|
|
|
|
|
|
|
// If there's no symbol, print out the offset in the section.
|
2019-01-10 15:08:06 +00:00
|
|
|
return (srcFile + ":(" + secAndOffset + ")");
|
2016-11-25 18:51:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 07:42:38 +00:00
|
|
|
// This function is intended to be used for constructing an error message.
|
|
|
|
// The returned message looks like this:
|
2017-03-30 19:13:47 +00:00
|
|
|
//
|
|
|
|
// foo.c:42 (/home/alice/possibly/very/long/path/foo.c:42)
|
|
|
|
//
|
2017-11-01 07:42:38 +00:00
|
|
|
// Returns an empty string if there's no way to get line info.
|
2017-11-03 21:21:47 +00:00
|
|
|
std::string InputSectionBase::getSrcMsg(const Symbol &sym, uint64_t offset) {
|
2017-12-23 17:21:39 +00:00
|
|
|
return file->getSrcMsg(sym, *this, offset);
|
2017-03-30 19:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a filename string along with an optional section name. This
|
|
|
|
// function is intended to be used for constructing an error
|
|
|
|
// message. The returned message looks like this:
|
|
|
|
//
|
|
|
|
// path/to/foo.o:(function bar)
|
|
|
|
//
|
|
|
|
// or
|
|
|
|
//
|
|
|
|
// path/to/foo.o:(function bar) in archive path/to/bar.a
|
2017-10-27 03:13:54 +00:00
|
|
|
std::string InputSectionBase::getObjMsg(uint64_t off) {
|
2017-07-05 14:55:43 +00:00
|
|
|
std::string filename = file->getName();
|
2017-03-30 19:13:47 +00:00
|
|
|
|
|
|
|
std::string archive;
|
|
|
|
if (!file->archiveName.empty())
|
2018-02-16 03:26:53 +00:00
|
|
|
archive = " in archive " + file->archiveName;
|
2017-03-30 19:13:47 +00:00
|
|
|
|
|
|
|
// Find a symbol that encloses a given location.
|
2017-11-03 21:21:47 +00:00
|
|
|
for (Symbol *b : file->getSymbols())
|
2017-11-06 04:35:31 +00:00
|
|
|
if (auto *d = dyn_cast<Defined>(b))
|
2017-03-30 19:13:47 +00:00
|
|
|
if (d->section == this && d->value <= off && off < d->value + d->size)
|
|
|
|
return filename + ":(" + toString(*d) + ")" + archive;
|
|
|
|
|
|
|
|
// If there's no symbol, print out the offset in the section.
|
|
|
|
return (filename + ":(" + name + "+0x" + utohexstr(off) + ")" + archive)
|
|
|
|
.str();
|
|
|
|
}
|
|
|
|
|
2017-12-21 02:11:51 +00:00
|
|
|
InputSection InputSection::discarded(nullptr, 0, 0, 0, ArrayRef<uint8_t>(), "");
|
2017-02-23 16:49:07 +00:00
|
|
|
|
2017-12-21 02:11:51 +00:00
|
|
|
InputSection::InputSection(InputFile *f, uint64_t flags, uint32_t type,
|
|
|
|
uint32_t alignment, ArrayRef<uint8_t> data,
|
|
|
|
StringRef name, Kind k)
|
|
|
|
: InputSectionBase(f, flags, type,
|
2017-03-07 15:11:21 +00:00
|
|
|
/*Entsize*/ 0, /*Link*/ 0, /*Info*/ 0, alignment, data,
|
2017-02-23 02:28:28 +00:00
|
|
|
name, k) {}
|
2016-10-26 00:54:03 +00:00
|
|
|
|
2015-07-24 21:03:07 +00:00
|
|
|
template <class ELFT>
|
2017-12-21 02:03:39 +00:00
|
|
|
InputSection::InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
|
2017-07-26 22:13:32 +00:00
|
|
|
StringRef name)
|
2017-02-23 02:28:28 +00:00
|
|
|
: InputSectionBase(f, header, name, InputSectionBase::Regular) {}
|
2015-10-19 21:00:02 +00:00
|
|
|
|
2017-03-08 22:36:28 +00:00
|
|
|
bool InputSection::classof(const SectionBase *s) {
|
|
|
|
return s->kind() == SectionBase::Regular ||
|
|
|
|
s->kind() == SectionBase::Synthetic;
|
|
|
|
}
|
|
|
|
|
2017-05-31 20:17:44 +00:00
|
|
|
OutputSection *InputSection::getParent() const {
|
|
|
|
return cast_or_null<OutputSection>(parent);
|
|
|
|
}
|
|
|
|
|
2017-06-09 03:19:08 +00:00
|
|
|
// Copy SHT_GROUP section contents. Used only for the -r option.
|
|
|
|
template <class ELFT> void InputSection::copyShtGroup(uint8_t *buf) {
|
|
|
|
// ELFT::Word is the 32-bit integral type in the target endianness.
|
2019-04-01 00:11:24 +00:00
|
|
|
using u32 = typename ELFT::Word;
|
2017-06-09 03:19:08 +00:00
|
|
|
ArrayRef<u32> from = getDataAs<u32>();
|
|
|
|
auto *to = reinterpret_cast<u32 *>(buf);
|
|
|
|
|
|
|
|
// The first entry is not a section number but a flag.
|
2017-05-29 08:37:50 +00:00
|
|
|
*to++ = from[0];
|
|
|
|
|
2017-06-09 03:19:08 +00:00
|
|
|
// Adjust section numbers because section numbers in an input object
|
|
|
|
// files are different in the output.
|
2017-12-19 16:29:02 +00:00
|
|
|
ArrayRef<InputSectionBase *> sections = file->getSections();
|
2017-06-09 03:19:08 +00:00
|
|
|
for (uint32_t idx : from.slice(1))
|
|
|
|
*to++ = sections[idx]->getOutputSection()->sectionIndex;
|
2017-05-29 08:37:50 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 01:58:43 +00:00
|
|
|
InputSectionBase *InputSection::getRelocatedSection() const {
|
2018-03-26 18:49:31 +00:00
|
|
|
if (!file || (type != SHT_RELA && type != SHT_REL))
|
|
|
|
return nullptr;
|
2017-12-19 16:29:02 +00:00
|
|
|
ArrayRef<InputSectionBase *> sections = file->getSections();
|
|
|
|
return sections[info];
|
2016-02-25 08:23:37 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 16:18:10 +00:00
|
|
|
// This is used for -r and --emit-relocs. We can't use memcpy to copy
|
|
|
|
// relocations because we need to update symbol table offset and section index
|
|
|
|
// for each relocation. So we copy relocations one by one.
|
2017-02-23 16:49:07 +00:00
|
|
|
template <class ELFT, class RelTy>
|
|
|
|
void InputSection::copyRelocations(uint8_t *buf, ArrayRef<RelTy> rels) {
|
2017-10-10 04:53:14 +00:00
|
|
|
InputSectionBase *sec = getRelocatedSection();
|
|
|
|
|
2016-03-13 05:06:50 +00:00
|
|
|
for (const RelTy &rel : rels) {
|
2017-10-11 22:49:24 +00:00
|
|
|
RelType type = rel.getType(config->isMips64EL);
|
2019-06-26 08:09:08 +00:00
|
|
|
const ObjFile<ELFT> *file = getFile<ELFT>();
|
|
|
|
Symbol &sym = file->getRelocTargetSym(rel);
|
2016-02-25 08:23:37 +00:00
|
|
|
|
2017-02-23 16:49:07 +00:00
|
|
|
auto *p = reinterpret_cast<typename ELFT::Rela *>(buf);
|
2016-03-13 05:06:50 +00:00
|
|
|
buf += sizeof(RelTy);
|
2016-02-25 08:23:37 +00:00
|
|
|
|
2018-02-02 09:50:07 +00:00
|
|
|
if (RelTy::IsRela)
|
2016-08-02 08:49:57 +00:00
|
|
|
p->r_addend = getAddend<ELFT>(rel);
|
2017-02-08 16:18:10 +00:00
|
|
|
|
2017-02-15 01:53:23 +00:00
|
|
|
// Output section VA is zero for -r, so r_offset is an offset within the
|
|
|
|
// section, but for --emit-relocs it is an virtual address.
|
2018-03-24 00:35:11 +00:00
|
|
|
p->r_offset = sec->getVA(rel.r_offset);
|
2018-09-25 19:26:58 +00:00
|
|
|
p->setSymbolAndType(in.symTab->getSymbolIndex(&sym), type,
|
2017-03-17 23:29:01 +00:00
|
|
|
config->isMips64EL);
|
2017-02-15 01:53:23 +00:00
|
|
|
|
2017-11-04 00:31:04 +00:00
|
|
|
if (sym.type == STT_SECTION) {
|
2017-02-11 01:40:49 +00:00
|
|
|
// We combine multiple section symbols into only one per
|
|
|
|
// section. This means we have to update the addend. That is
|
|
|
|
// trivial for Elf_Rela, but for Elf_Rel we have to write to the
|
|
|
|
// section data. We do that by adding to the Relocation vector.
|
2017-02-15 00:59:50 +00:00
|
|
|
|
|
|
|
// .eh_frame is horribly special and can reference discarded sections. To
|
|
|
|
// avoid having to parse and recreate .eh_frame, we just replace any
|
|
|
|
// relocation in it pointing to discarded sections with R_*_NONE, which
|
2019-06-26 08:09:08 +00:00
|
|
|
// hopefully creates a frame that is ignored at runtime. Also, don't warn
|
|
|
|
// on .gcc_except_table and debug sections.
|
|
|
|
//
|
|
|
|
// See the comment in maybeReportUndefined for PPC64 .toc .
|
2017-11-30 06:18:31 +00:00
|
|
|
auto *d = dyn_cast<Defined>(&sym);
|
|
|
|
if (!d) {
|
2019-06-26 08:09:08 +00:00
|
|
|
if (!sec->name.startswith(".debug") &&
|
|
|
|
!sec->name.startswith(".zdebug") && sec->name != ".eh_frame" &&
|
|
|
|
sec->name != ".gcc_except_table" && sec->name != ".toc") {
|
|
|
|
uint32_t secIdx = cast<Undefined>(sym).discardedSecIdx;
|
|
|
|
Elf_Shdr_Impl<ELFT> sec =
|
|
|
|
CHECK(file->getObj().sections(), file)[secIdx];
|
|
|
|
warn("relocation refers to a discarded section: " +
|
|
|
|
CHECK(file->getObj().getSectionName(&sec), file) +
|
|
|
|
"\n>>> referenced by " + getObjMsg(p->r_offset));
|
|
|
|
}
|
|
|
|
p->setSymbolAndType(0, 0, false);
|
2017-11-30 06:18:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-11-01 09:20:06 +00:00
|
|
|
SectionBase *section = d->section->repl;
|
2019-05-29 03:55:20 +00:00
|
|
|
if (!section->isLive()) {
|
2017-02-15 00:59:50 +00:00
|
|
|
p->setSymbolAndType(0, 0, false);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-08 15:34:06 +00:00
|
|
|
int64_t addend = getAddend<ELFT>(rel);
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
const uint8_t *bufLoc = sec->data().begin() + rel.r_offset;
|
2018-05-08 15:34:06 +00:00
|
|
|
if (!RelTy::IsRela)
|
|
|
|
addend = target->getImplicitAddend(bufLoc, type);
|
|
|
|
|
|
|
|
if (config->emachine == EM_MIPS && config->relocatable &&
|
|
|
|
target->getRelExpr(type, sym, bufLoc) == R_MIPS_GOTREL) {
|
|
|
|
// Some MIPS relocations depend on "gp" value. By default,
|
|
|
|
// this value has 0x7ff0 offset from a .got section. But
|
2019-10-29 01:41:38 +00:00
|
|
|
// relocatable files produced by a compiler or a linker
|
2018-05-08 15:34:06 +00:00
|
|
|
// might redefine this default value and we must use it
|
|
|
|
// for a calculation of the relocation result. When we
|
|
|
|
// generate EXE or DSO it's trivial. Generating a relocatable
|
|
|
|
// output is more difficult case because the linker does
|
|
|
|
// not calculate relocations in this mode and loses
|
|
|
|
// individual "gp" values used by each input object file.
|
|
|
|
// As a workaround we add the "gp" value to the relocation
|
|
|
|
// addend and save it back to the file.
|
|
|
|
addend += sec->getFile<ELFT>()->mipsGp0;
|
2017-02-11 01:40:49 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 15:34:06 +00:00
|
|
|
if (RelTy::IsRela)
|
2018-11-01 09:20:06 +00:00
|
|
|
p->r_addend = sym.getVA(addend) - section->getOutputSection()->addr;
|
2019-05-17 14:11:03 +00:00
|
|
|
else if (config->relocatable && type != target->noneRel)
|
2018-05-08 15:34:06 +00:00
|
|
|
sec->relocations.push_back({R_ABS, type, rel.r_offset, addend, &sym});
|
|
|
|
}
|
2016-02-25 08:23:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 09:43:43 +00:00
|
|
|
// The ARM and AArch64 ABI handle pc-relative relocations to undefined weak
|
|
|
|
// references specially. The general rule is that the value of the symbol in
|
|
|
|
// this context is the address of the place P. A further special case is that
|
|
|
|
// branch relocations to an undefined weak reference resolve to the next
|
|
|
|
// instruction.
|
2017-10-11 22:49:24 +00:00
|
|
|
static uint32_t getARMUndefinedRelativeWeakVA(RelType type, uint32_t a,
|
2016-11-09 10:22:29 +00:00
|
|
|
uint32_t p) {
|
|
|
|
switch (type) {
|
2017-06-19 09:43:43 +00:00
|
|
|
// Unresolved branch relocations to weak references resolve to next
|
|
|
|
// instruction, this will be either 2 or 4 bytes on from P.
|
2016-11-09 10:22:29 +00:00
|
|
|
case R_ARM_THM_JUMP11:
|
2017-06-12 18:05:01 +00:00
|
|
|
return p + 2 + a;
|
2016-11-09 10:22:29 +00:00
|
|
|
case R_ARM_CALL:
|
|
|
|
case R_ARM_JUMP24:
|
|
|
|
case R_ARM_PC24:
|
|
|
|
case R_ARM_PLT32:
|
|
|
|
case R_ARM_PREL31:
|
|
|
|
case R_ARM_THM_JUMP19:
|
|
|
|
case R_ARM_THM_JUMP24:
|
2017-06-12 18:05:01 +00:00
|
|
|
return p + 4 + a;
|
2016-11-09 10:22:29 +00:00
|
|
|
case R_ARM_THM_CALL:
|
|
|
|
// We don't want an interworking BLX to ARM
|
2017-06-12 18:05:01 +00:00
|
|
|
return p + 5 + a;
|
2017-06-19 09:43:43 +00:00
|
|
|
// Unresolved non branch pc-relative relocations
|
|
|
|
// R_ARM_TARGET2 which can be resolved relatively is not present as it never
|
|
|
|
// targets a weak-reference.
|
|
|
|
case R_ARM_MOVW_PREL_NC:
|
|
|
|
case R_ARM_MOVT_PREL:
|
|
|
|
case R_ARM_REL32:
|
|
|
|
case R_ARM_THM_MOVW_PREL_NC:
|
|
|
|
case R_ARM_THM_MOVT_PREL:
|
2017-06-12 18:05:01 +00:00
|
|
|
return p + a;
|
2016-11-09 10:22:29 +00:00
|
|
|
}
|
2017-06-19 09:43:43 +00:00
|
|
|
llvm_unreachable("ARM pc-relative relocation expected\n");
|
2016-11-09 10:22:29 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 09:43:43 +00:00
|
|
|
// The comment above getARMUndefinedRelativeWeakVA applies to this function.
|
2016-11-14 10:14:18 +00:00
|
|
|
static uint64_t getAArch64UndefinedRelativeWeakVA(uint64_t type, uint64_t a,
|
2016-11-09 10:22:29 +00:00
|
|
|
uint64_t p) {
|
|
|
|
switch (type) {
|
2017-06-19 09:43:43 +00:00
|
|
|
// Unresolved branch relocations to weak references resolve to next
|
|
|
|
// instruction, this is 4 bytes on from P.
|
2016-11-09 10:22:29 +00:00
|
|
|
case R_AARCH64_CALL26:
|
|
|
|
case R_AARCH64_CONDBR19:
|
|
|
|
case R_AARCH64_JUMP26:
|
|
|
|
case R_AARCH64_TSTBR14:
|
2017-06-12 18:05:01 +00:00
|
|
|
return p + 4 + a;
|
2017-06-19 09:43:43 +00:00
|
|
|
// Unresolved non branch pc-relative relocations
|
|
|
|
case R_AARCH64_PREL16:
|
|
|
|
case R_AARCH64_PREL32:
|
|
|
|
case R_AARCH64_PREL64:
|
|
|
|
case R_AARCH64_ADR_PREL_LO21:
|
2017-09-20 23:49:50 +00:00
|
|
|
case R_AARCH64_LD_PREL_LO19:
|
2017-06-12 18:05:01 +00:00
|
|
|
return p + a;
|
2016-11-09 10:22:29 +00:00
|
|
|
}
|
2017-06-19 09:43:43 +00:00
|
|
|
llvm_unreachable("AArch64 pc-relative relocation expected\n");
|
2016-11-09 10:22:29 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 09:12:21 +00:00
|
|
|
// ARM SBREL relocations are of the form S + A - B where B is the static base
|
|
|
|
// The ARM ABI defines base to be "addressing origin of the output segment
|
|
|
|
// defining the symbol S". We defined the "addressing origin"/static base to be
|
2017-11-04 00:31:04 +00:00
|
|
|
// the base of the PT_LOAD segment containing the Sym.
|
2017-05-18 09:12:21 +00:00
|
|
|
// The procedure call standard only defines a Read Write Position Independent
|
|
|
|
// RWPI variant so in practice we should expect the static base to be the base
|
|
|
|
// of the RW segment.
|
2017-11-04 00:31:04 +00:00
|
|
|
static uint64_t getARMStaticBase(const Symbol &sym) {
|
|
|
|
OutputSection *os = sym.getOutputSection();
|
2017-09-07 11:01:10 +00:00
|
|
|
if (!os || !os->ptLoad || !os->ptLoad->firstSec)
|
2017-11-04 00:31:04 +00:00
|
|
|
fatal("SBREL relocation to " + sym.getName() + " without static base");
|
2017-09-07 11:01:10 +00:00
|
|
|
return os->ptLoad->firstSec->addr;
|
2017-05-18 09:12:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 17:59:56 +00:00
|
|
|
// For R_RISCV_PC_INDIRECT (R_RISCV_PCREL_LO12_{I,S}), the symbol actually
|
|
|
|
// points the corresponding R_RISCV_PCREL_HI20 relocation, and the target VA
|
|
|
|
// is calculated using PCREL_HI20's symbol.
|
|
|
|
//
|
|
|
|
// This function returns the R_RISCV_PCREL_HI20 relocation from
|
|
|
|
// R_RISCV_PCREL_LO12's symbol and addend.
|
2018-11-23 15:13:26 +00:00
|
|
|
static Relocation *getRISCVPCRelHi20(const Symbol *sym, uint64_t addend) {
|
2018-08-09 17:59:56 +00:00
|
|
|
const Defined *d = cast<Defined>(sym);
|
2019-07-03 15:38:59 +00:00
|
|
|
if (!d->section) {
|
|
|
|
error("R_RISCV_PCREL_LO12 relocation points to an absolute symbol: " +
|
|
|
|
sym->getName());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-08-09 17:59:56 +00:00
|
|
|
InputSection *isec = cast<InputSection>(d->section);
|
|
|
|
|
|
|
|
if (addend != 0)
|
|
|
|
warn("Non-zero addend in R_RISCV_PCREL_LO12 relocation to " +
|
|
|
|
isec->getObjMsg(d->value) + " is ignored");
|
|
|
|
|
|
|
|
// Relocations are sorted by offset, so we can use std::equal_range to do
|
|
|
|
// binary search.
|
2019-02-14 19:21:10 +00:00
|
|
|
Relocation r;
|
|
|
|
r.offset = d->value;
|
|
|
|
auto range =
|
|
|
|
std::equal_range(isec->relocations.begin(), isec->relocations.end(), r,
|
|
|
|
[](const Relocation &lhs, const Relocation &rhs) {
|
|
|
|
return lhs.offset < rhs.offset;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (auto it = range.first; it != range.second; ++it)
|
2019-07-01 17:12:26 +00:00
|
|
|
if (it->type == R_RISCV_PCREL_HI20 || it->type == R_RISCV_GOT_HI20 ||
|
|
|
|
it->type == R_RISCV_TLS_GD_HI20 || it->type == R_RISCV_TLS_GOT_HI20)
|
2018-08-09 17:59:56 +00:00
|
|
|
return &*it;
|
|
|
|
|
|
|
|
error("R_RISCV_PCREL_LO12 relocation points to " + isec->getObjMsg(d->value) +
|
|
|
|
" without an associated R_RISCV_PCREL_HI20 relocation");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
[ELF] Refactor per-target TLS layout configuration. NFC.
Summary:
There are really three different kinds of TLS layouts:
* A fixed TLS-to-TP offset. On architectures like PowerPC, MIPS, and
RISC-V, the thread pointer points to a fixed offset from the start
of the executable's TLS segment. The offset is 0x7000 for PowerPC
and MIPS, which allows a signed 16-bit offset to reach 0x1000 of
per-thread implementation data and 0xf000 of the application's TLS
segment. The size and layout of the TCB isn't relevant to the static
linker and might not be known.
* A fixed TCB size. This is the format documented as "variant 1" in
Ulrich Drepper's TLS spec. The thread pointer points to a 2-word TCB
followed by the executable's TLS segment. The first word is always
the DTV pointer. Used on ARM. The thread pointer must be aligned to
the TLS segment's alignment, possibly creating alignment padding.
* Variant 2. This format predates variant 1 and is also documented in
Drepper's TLS spec. It allocates the executable's TLS segment before
the thread pointer, apparently for backwards-compatibility. It's
used on x86 and SPARC.
Factor out an lld::elf::getTlsTpOffset() function for use in a
follow-up patch for Android. The TcbSize/TlsTpOffset fields are only used
in getTlsTpOffset, so replace them with a switch on Config->EMachine.
Reviewers: espindola, ruiu, PkmX, jrtc27
Reviewed By: ruiu, PkmX, jrtc27
Subscribers: jyknight, emaste, sdardis, nemanjai, javed.absar, arichardson, kristof.beyls, kbarton, fedor.sergeev, atanasyan, PkmX, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D53905
llvm-svn: 345775
2018-10-31 20:53:17 +00:00
|
|
|
// A TLS symbol's virtual address is relative to the TLS segment. Add a
|
|
|
|
// target-specific adjustment to produce a thread-pointer-relative offset.
|
2019-05-30 10:00:20 +00:00
|
|
|
static int64_t getTlsTpOffset(const Symbol &s) {
|
|
|
|
// On targets that support TLSDESC, _TLS_MODULE_BASE_@tpoff = 0.
|
|
|
|
if (&s == ElfSym::tlsModuleBase)
|
|
|
|
return 0;
|
|
|
|
|
[ELF][PPC] Allow PT_LOAD to have overlapping p_offset ranges
This change affects the non-linker script case (precisely, when the
`SECTIONS` command is not used). It deletes 3 alignments at PT_LOAD
boundaries for the default case: the size of a powerpc64 binary can be
decreased by at most 192kb. The technique can be ported to other
targets.
Let me demonstrate the idea with a maxPageSize=65536 example:
When assigning the address to the first output section of a new PT_LOAD,
if the end p_vaddr of the previous PT_LOAD is 0x10020, we advance to
the next multiple of maxPageSize: 0x20000. The new PT_LOAD will thus
have p_vaddr=0x20000. Because p_offset and p_vaddr are congruent modulo
maxPageSize, p_offset will be 0x20000, leaving a p_offset gap [0x10020,
0x20000) in the output.
Alternatively, if we advance to 0x20020, the new PT_LOAD will have
p_vaddr=0x20020. We can pick either 0x10020 or 0x20020 for p_offset!
Obviously 0x10020 is the choice because it leaves no gap. At runtime,
p_vaddr will be rounded down by pagesize (65536 if
pagesize=maxPageSize). This PT_LOAD will load additional initial
contents from p_offset ranges [0x10000,0x10020), which will also be
loaded by the previous PT_LOAD. This is fine if -z noseparate-code is in
effect or if we are not transiting between executable and non-executable
segments.
ld.bfd -z noseparate-code leverages this technique to keep output small.
This patch implements the technique in lld, which is mostly effective on
targets with large defaultMaxPageSize (AArch64/MIPS/PPC: 65536). The 3
removed alignments can save almost 3*65536 bytes.
Two places that rely on p_vaddr%pagesize = 0 have to be updated.
1) We used to round p_memsz(PT_GNU_RELRO) up to commonPageSize (defaults
to 4096 on all targets). Now p_vaddr%commonPageSize may be non-zero.
The updated formula takes account of that factor.
2) Our TP offsets formulae are only correct if p_vaddr%p_align = 0.
Fix them. See the updated comments in InputSection.cpp for details.
On targets that we enable the technique (only PPC64 now),
we can potentially make `p_vaddr(PT_TLS)%p_align(PT_TLS) != 0`
if `sh_addralign(.tdata) < sh_addralign(.tbss)`
This exposes many problems in ld.so implementations, especially the
offsets of dynamic TLS blocks. Known issues:
FreeBSD 13.0-CURRENT rtld-elf (i386/amd64/powerpc/arm64)
glibc (HEAD) i386 and x86_64 https://sourceware.org/bugzilla/show_bug.cgi?id=24606
musl<=1.1.22 on TLS Variant I architectures (aarch64/powerpc64/...)
So, force p_vaddr%p_align = 0 by rounding dot up to p_align(PT_TLS).
The technique will be enabled (with updated tests) for other targets in
subsequent patches.
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D64906
llvm-svn: 369343
2019-08-20 08:34:25 +00:00
|
|
|
// There are 2 TLS layouts. Among targets we support, x86 uses TLS Variant 2
|
|
|
|
// while most others use Variant 1. At run time TP will be aligned to p_align.
|
|
|
|
|
|
|
|
// Variant 1. TP will be followed by an optional gap (which is the size of 2
|
|
|
|
// pointers on ARM/AArch64, 0 on other targets), followed by alignment
|
|
|
|
// padding, then the static TLS blocks. The alignment padding is added so that
|
|
|
|
// (TP + gap + padding) is congruent to p_vaddr modulo p_align.
|
|
|
|
//
|
|
|
|
// Variant 2. Static TLS blocks, followed by alignment padding are placed
|
|
|
|
// before TP. The alignment padding is added so that (TP - padding -
|
|
|
|
// p_memsz) is congruent to p_vaddr modulo p_align.
|
2019-10-07 08:31:18 +00:00
|
|
|
PhdrEntry *tls = Out::tlsPhdr;
|
[ELF] Refactor per-target TLS layout configuration. NFC.
Summary:
There are really three different kinds of TLS layouts:
* A fixed TLS-to-TP offset. On architectures like PowerPC, MIPS, and
RISC-V, the thread pointer points to a fixed offset from the start
of the executable's TLS segment. The offset is 0x7000 for PowerPC
and MIPS, which allows a signed 16-bit offset to reach 0x1000 of
per-thread implementation data and 0xf000 of the application's TLS
segment. The size and layout of the TCB isn't relevant to the static
linker and might not be known.
* A fixed TCB size. This is the format documented as "variant 1" in
Ulrich Drepper's TLS spec. The thread pointer points to a 2-word TCB
followed by the executable's TLS segment. The first word is always
the DTV pointer. Used on ARM. The thread pointer must be aligned to
the TLS segment's alignment, possibly creating alignment padding.
* Variant 2. This format predates variant 1 and is also documented in
Drepper's TLS spec. It allocates the executable's TLS segment before
the thread pointer, apparently for backwards-compatibility. It's
used on x86 and SPARC.
Factor out an lld::elf::getTlsTpOffset() function for use in a
follow-up patch for Android. The TcbSize/TlsTpOffset fields are only used
in getTlsTpOffset, so replace them with a switch on Config->EMachine.
Reviewers: espindola, ruiu, PkmX, jrtc27
Reviewed By: ruiu, PkmX, jrtc27
Subscribers: jyknight, emaste, sdardis, nemanjai, javed.absar, arichardson, kristof.beyls, kbarton, fedor.sergeev, atanasyan, PkmX, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D53905
llvm-svn: 345775
2018-10-31 20:53:17 +00:00
|
|
|
switch (config->emachine) {
|
[ELF][PPC] Allow PT_LOAD to have overlapping p_offset ranges
This change affects the non-linker script case (precisely, when the
`SECTIONS` command is not used). It deletes 3 alignments at PT_LOAD
boundaries for the default case: the size of a powerpc64 binary can be
decreased by at most 192kb. The technique can be ported to other
targets.
Let me demonstrate the idea with a maxPageSize=65536 example:
When assigning the address to the first output section of a new PT_LOAD,
if the end p_vaddr of the previous PT_LOAD is 0x10020, we advance to
the next multiple of maxPageSize: 0x20000. The new PT_LOAD will thus
have p_vaddr=0x20000. Because p_offset and p_vaddr are congruent modulo
maxPageSize, p_offset will be 0x20000, leaving a p_offset gap [0x10020,
0x20000) in the output.
Alternatively, if we advance to 0x20020, the new PT_LOAD will have
p_vaddr=0x20020. We can pick either 0x10020 or 0x20020 for p_offset!
Obviously 0x10020 is the choice because it leaves no gap. At runtime,
p_vaddr will be rounded down by pagesize (65536 if
pagesize=maxPageSize). This PT_LOAD will load additional initial
contents from p_offset ranges [0x10000,0x10020), which will also be
loaded by the previous PT_LOAD. This is fine if -z noseparate-code is in
effect or if we are not transiting between executable and non-executable
segments.
ld.bfd -z noseparate-code leverages this technique to keep output small.
This patch implements the technique in lld, which is mostly effective on
targets with large defaultMaxPageSize (AArch64/MIPS/PPC: 65536). The 3
removed alignments can save almost 3*65536 bytes.
Two places that rely on p_vaddr%pagesize = 0 have to be updated.
1) We used to round p_memsz(PT_GNU_RELRO) up to commonPageSize (defaults
to 4096 on all targets). Now p_vaddr%commonPageSize may be non-zero.
The updated formula takes account of that factor.
2) Our TP offsets formulae are only correct if p_vaddr%p_align = 0.
Fix them. See the updated comments in InputSection.cpp for details.
On targets that we enable the technique (only PPC64 now),
we can potentially make `p_vaddr(PT_TLS)%p_align(PT_TLS) != 0`
if `sh_addralign(.tdata) < sh_addralign(.tbss)`
This exposes many problems in ld.so implementations, especially the
offsets of dynamic TLS blocks. Known issues:
FreeBSD 13.0-CURRENT rtld-elf (i386/amd64/powerpc/arm64)
glibc (HEAD) i386 and x86_64 https://sourceware.org/bugzilla/show_bug.cgi?id=24606
musl<=1.1.22 on TLS Variant I architectures (aarch64/powerpc64/...)
So, force p_vaddr%p_align = 0 by rounding dot up to p_align(PT_TLS).
The technique will be enabled (with updated tests) for other targets in
subsequent patches.
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D64906
llvm-svn: 369343
2019-08-20 08:34:25 +00:00
|
|
|
// Variant 1.
|
[ELF] Refactor per-target TLS layout configuration. NFC.
Summary:
There are really three different kinds of TLS layouts:
* A fixed TLS-to-TP offset. On architectures like PowerPC, MIPS, and
RISC-V, the thread pointer points to a fixed offset from the start
of the executable's TLS segment. The offset is 0x7000 for PowerPC
and MIPS, which allows a signed 16-bit offset to reach 0x1000 of
per-thread implementation data and 0xf000 of the application's TLS
segment. The size and layout of the TCB isn't relevant to the static
linker and might not be known.
* A fixed TCB size. This is the format documented as "variant 1" in
Ulrich Drepper's TLS spec. The thread pointer points to a 2-word TCB
followed by the executable's TLS segment. The first word is always
the DTV pointer. Used on ARM. The thread pointer must be aligned to
the TLS segment's alignment, possibly creating alignment padding.
* Variant 2. This format predates variant 1 and is also documented in
Drepper's TLS spec. It allocates the executable's TLS segment before
the thread pointer, apparently for backwards-compatibility. It's
used on x86 and SPARC.
Factor out an lld::elf::getTlsTpOffset() function for use in a
follow-up patch for Android. The TcbSize/TlsTpOffset fields are only used
in getTlsTpOffset, so replace them with a switch on Config->EMachine.
Reviewers: espindola, ruiu, PkmX, jrtc27
Reviewed By: ruiu, PkmX, jrtc27
Subscribers: jyknight, emaste, sdardis, nemanjai, javed.absar, arichardson, kristof.beyls, kbarton, fedor.sergeev, atanasyan, PkmX, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D53905
llvm-svn: 345775
2018-10-31 20:53:17 +00:00
|
|
|
case EM_ARM:
|
|
|
|
case EM_AARCH64:
|
[ELF][PPC] Allow PT_LOAD to have overlapping p_offset ranges
This change affects the non-linker script case (precisely, when the
`SECTIONS` command is not used). It deletes 3 alignments at PT_LOAD
boundaries for the default case: the size of a powerpc64 binary can be
decreased by at most 192kb. The technique can be ported to other
targets.
Let me demonstrate the idea with a maxPageSize=65536 example:
When assigning the address to the first output section of a new PT_LOAD,
if the end p_vaddr of the previous PT_LOAD is 0x10020, we advance to
the next multiple of maxPageSize: 0x20000. The new PT_LOAD will thus
have p_vaddr=0x20000. Because p_offset and p_vaddr are congruent modulo
maxPageSize, p_offset will be 0x20000, leaving a p_offset gap [0x10020,
0x20000) in the output.
Alternatively, if we advance to 0x20020, the new PT_LOAD will have
p_vaddr=0x20020. We can pick either 0x10020 or 0x20020 for p_offset!
Obviously 0x10020 is the choice because it leaves no gap. At runtime,
p_vaddr will be rounded down by pagesize (65536 if
pagesize=maxPageSize). This PT_LOAD will load additional initial
contents from p_offset ranges [0x10000,0x10020), which will also be
loaded by the previous PT_LOAD. This is fine if -z noseparate-code is in
effect or if we are not transiting between executable and non-executable
segments.
ld.bfd -z noseparate-code leverages this technique to keep output small.
This patch implements the technique in lld, which is mostly effective on
targets with large defaultMaxPageSize (AArch64/MIPS/PPC: 65536). The 3
removed alignments can save almost 3*65536 bytes.
Two places that rely on p_vaddr%pagesize = 0 have to be updated.
1) We used to round p_memsz(PT_GNU_RELRO) up to commonPageSize (defaults
to 4096 on all targets). Now p_vaddr%commonPageSize may be non-zero.
The updated formula takes account of that factor.
2) Our TP offsets formulae are only correct if p_vaddr%p_align = 0.
Fix them. See the updated comments in InputSection.cpp for details.
On targets that we enable the technique (only PPC64 now),
we can potentially make `p_vaddr(PT_TLS)%p_align(PT_TLS) != 0`
if `sh_addralign(.tdata) < sh_addralign(.tbss)`
This exposes many problems in ld.so implementations, especially the
offsets of dynamic TLS blocks. Known issues:
FreeBSD 13.0-CURRENT rtld-elf (i386/amd64/powerpc/arm64)
glibc (HEAD) i386 and x86_64 https://sourceware.org/bugzilla/show_bug.cgi?id=24606
musl<=1.1.22 on TLS Variant I architectures (aarch64/powerpc64/...)
So, force p_vaddr%p_align = 0 by rounding dot up to p_align(PT_TLS).
The technique will be enabled (with updated tests) for other targets in
subsequent patches.
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D64906
llvm-svn: 369343
2019-08-20 08:34:25 +00:00
|
|
|
return s.getVA(0) + config->wordsize * 2 +
|
|
|
|
((tls->p_vaddr - config->wordsize * 2) & (tls->p_align - 1));
|
2019-07-24 11:37:13 +00:00
|
|
|
case EM_MIPS:
|
2019-06-06 17:03:10 +00:00
|
|
|
case EM_PPC:
|
[ELF] Refactor per-target TLS layout configuration. NFC.
Summary:
There are really three different kinds of TLS layouts:
* A fixed TLS-to-TP offset. On architectures like PowerPC, MIPS, and
RISC-V, the thread pointer points to a fixed offset from the start
of the executable's TLS segment. The offset is 0x7000 for PowerPC
and MIPS, which allows a signed 16-bit offset to reach 0x1000 of
per-thread implementation data and 0xf000 of the application's TLS
segment. The size and layout of the TCB isn't relevant to the static
linker and might not be known.
* A fixed TCB size. This is the format documented as "variant 1" in
Ulrich Drepper's TLS spec. The thread pointer points to a 2-word TCB
followed by the executable's TLS segment. The first word is always
the DTV pointer. Used on ARM. The thread pointer must be aligned to
the TLS segment's alignment, possibly creating alignment padding.
* Variant 2. This format predates variant 1 and is also documented in
Drepper's TLS spec. It allocates the executable's TLS segment before
the thread pointer, apparently for backwards-compatibility. It's
used on x86 and SPARC.
Factor out an lld::elf::getTlsTpOffset() function for use in a
follow-up patch for Android. The TcbSize/TlsTpOffset fields are only used
in getTlsTpOffset, so replace them with a switch on Config->EMachine.
Reviewers: espindola, ruiu, PkmX, jrtc27
Reviewed By: ruiu, PkmX, jrtc27
Subscribers: jyknight, emaste, sdardis, nemanjai, javed.absar, arichardson, kristof.beyls, kbarton, fedor.sergeev, atanasyan, PkmX, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D53905
llvm-svn: 345775
2018-10-31 20:53:17 +00:00
|
|
|
case EM_PPC64:
|
[ELF][PPC] Allow PT_LOAD to have overlapping p_offset ranges
This change affects the non-linker script case (precisely, when the
`SECTIONS` command is not used). It deletes 3 alignments at PT_LOAD
boundaries for the default case: the size of a powerpc64 binary can be
decreased by at most 192kb. The technique can be ported to other
targets.
Let me demonstrate the idea with a maxPageSize=65536 example:
When assigning the address to the first output section of a new PT_LOAD,
if the end p_vaddr of the previous PT_LOAD is 0x10020, we advance to
the next multiple of maxPageSize: 0x20000. The new PT_LOAD will thus
have p_vaddr=0x20000. Because p_offset and p_vaddr are congruent modulo
maxPageSize, p_offset will be 0x20000, leaving a p_offset gap [0x10020,
0x20000) in the output.
Alternatively, if we advance to 0x20020, the new PT_LOAD will have
p_vaddr=0x20020. We can pick either 0x10020 or 0x20020 for p_offset!
Obviously 0x10020 is the choice because it leaves no gap. At runtime,
p_vaddr will be rounded down by pagesize (65536 if
pagesize=maxPageSize). This PT_LOAD will load additional initial
contents from p_offset ranges [0x10000,0x10020), which will also be
loaded by the previous PT_LOAD. This is fine if -z noseparate-code is in
effect or if we are not transiting between executable and non-executable
segments.
ld.bfd -z noseparate-code leverages this technique to keep output small.
This patch implements the technique in lld, which is mostly effective on
targets with large defaultMaxPageSize (AArch64/MIPS/PPC: 65536). The 3
removed alignments can save almost 3*65536 bytes.
Two places that rely on p_vaddr%pagesize = 0 have to be updated.
1) We used to round p_memsz(PT_GNU_RELRO) up to commonPageSize (defaults
to 4096 on all targets). Now p_vaddr%commonPageSize may be non-zero.
The updated formula takes account of that factor.
2) Our TP offsets formulae are only correct if p_vaddr%p_align = 0.
Fix them. See the updated comments in InputSection.cpp for details.
On targets that we enable the technique (only PPC64 now),
we can potentially make `p_vaddr(PT_TLS)%p_align(PT_TLS) != 0`
if `sh_addralign(.tdata) < sh_addralign(.tbss)`
This exposes many problems in ld.so implementations, especially the
offsets of dynamic TLS blocks. Known issues:
FreeBSD 13.0-CURRENT rtld-elf (i386/amd64/powerpc/arm64)
glibc (HEAD) i386 and x86_64 https://sourceware.org/bugzilla/show_bug.cgi?id=24606
musl<=1.1.22 on TLS Variant I architectures (aarch64/powerpc64/...)
So, force p_vaddr%p_align = 0 by rounding dot up to p_align(PT_TLS).
The technique will be enabled (with updated tests) for other targets in
subsequent patches.
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D64906
llvm-svn: 369343
2019-08-20 08:34:25 +00:00
|
|
|
// Adjusted Variant 1. TP is placed with a displacement of 0x7000, which is
|
|
|
|
// to allow a signed 16-bit offset to reach 0x1000 of TCB/thread-library
|
|
|
|
// data and 0xf000 of the program's TLS segment.
|
|
|
|
return s.getVA(0) + (tls->p_vaddr & (tls->p_align - 1)) - 0x7000;
|
2019-07-01 17:12:26 +00:00
|
|
|
case EM_RISCV:
|
[ELF][PPC] Allow PT_LOAD to have overlapping p_offset ranges
This change affects the non-linker script case (precisely, when the
`SECTIONS` command is not used). It deletes 3 alignments at PT_LOAD
boundaries for the default case: the size of a powerpc64 binary can be
decreased by at most 192kb. The technique can be ported to other
targets.
Let me demonstrate the idea with a maxPageSize=65536 example:
When assigning the address to the first output section of a new PT_LOAD,
if the end p_vaddr of the previous PT_LOAD is 0x10020, we advance to
the next multiple of maxPageSize: 0x20000. The new PT_LOAD will thus
have p_vaddr=0x20000. Because p_offset and p_vaddr are congruent modulo
maxPageSize, p_offset will be 0x20000, leaving a p_offset gap [0x10020,
0x20000) in the output.
Alternatively, if we advance to 0x20020, the new PT_LOAD will have
p_vaddr=0x20020. We can pick either 0x10020 or 0x20020 for p_offset!
Obviously 0x10020 is the choice because it leaves no gap. At runtime,
p_vaddr will be rounded down by pagesize (65536 if
pagesize=maxPageSize). This PT_LOAD will load additional initial
contents from p_offset ranges [0x10000,0x10020), which will also be
loaded by the previous PT_LOAD. This is fine if -z noseparate-code is in
effect or if we are not transiting between executable and non-executable
segments.
ld.bfd -z noseparate-code leverages this technique to keep output small.
This patch implements the technique in lld, which is mostly effective on
targets with large defaultMaxPageSize (AArch64/MIPS/PPC: 65536). The 3
removed alignments can save almost 3*65536 bytes.
Two places that rely on p_vaddr%pagesize = 0 have to be updated.
1) We used to round p_memsz(PT_GNU_RELRO) up to commonPageSize (defaults
to 4096 on all targets). Now p_vaddr%commonPageSize may be non-zero.
The updated formula takes account of that factor.
2) Our TP offsets formulae are only correct if p_vaddr%p_align = 0.
Fix them. See the updated comments in InputSection.cpp for details.
On targets that we enable the technique (only PPC64 now),
we can potentially make `p_vaddr(PT_TLS)%p_align(PT_TLS) != 0`
if `sh_addralign(.tdata) < sh_addralign(.tbss)`
This exposes many problems in ld.so implementations, especially the
offsets of dynamic TLS blocks. Known issues:
FreeBSD 13.0-CURRENT rtld-elf (i386/amd64/powerpc/arm64)
glibc (HEAD) i386 and x86_64 https://sourceware.org/bugzilla/show_bug.cgi?id=24606
musl<=1.1.22 on TLS Variant I architectures (aarch64/powerpc64/...)
So, force p_vaddr%p_align = 0 by rounding dot up to p_align(PT_TLS).
The technique will be enabled (with updated tests) for other targets in
subsequent patches.
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D64906
llvm-svn: 369343
2019-08-20 08:34:25 +00:00
|
|
|
return s.getVA(0) + (tls->p_vaddr & (tls->p_align - 1));
|
|
|
|
|
|
|
|
// Variant 2.
|
|
|
|
case EM_386:
|
|
|
|
case EM_X86_64:
|
|
|
|
return s.getVA(0) - tls->p_memsz -
|
|
|
|
((-tls->p_vaddr - tls->p_memsz) & (tls->p_align - 1));
|
[ELF] Refactor per-target TLS layout configuration. NFC.
Summary:
There are really three different kinds of TLS layouts:
* A fixed TLS-to-TP offset. On architectures like PowerPC, MIPS, and
RISC-V, the thread pointer points to a fixed offset from the start
of the executable's TLS segment. The offset is 0x7000 for PowerPC
and MIPS, which allows a signed 16-bit offset to reach 0x1000 of
per-thread implementation data and 0xf000 of the application's TLS
segment. The size and layout of the TCB isn't relevant to the static
linker and might not be known.
* A fixed TCB size. This is the format documented as "variant 1" in
Ulrich Drepper's TLS spec. The thread pointer points to a 2-word TCB
followed by the executable's TLS segment. The first word is always
the DTV pointer. Used on ARM. The thread pointer must be aligned to
the TLS segment's alignment, possibly creating alignment padding.
* Variant 2. This format predates variant 1 and is also documented in
Drepper's TLS spec. It allocates the executable's TLS segment before
the thread pointer, apparently for backwards-compatibility. It's
used on x86 and SPARC.
Factor out an lld::elf::getTlsTpOffset() function for use in a
follow-up patch for Android. The TcbSize/TlsTpOffset fields are only used
in getTlsTpOffset, so replace them with a switch on Config->EMachine.
Reviewers: espindola, ruiu, PkmX, jrtc27
Reviewed By: ruiu, PkmX, jrtc27
Subscribers: jyknight, emaste, sdardis, nemanjai, javed.absar, arichardson, kristof.beyls, kbarton, fedor.sergeev, atanasyan, PkmX, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D53905
llvm-svn: 345775
2018-10-31 20:53:17 +00:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unhandled Config->EMachine");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 08:37:19 +00:00
|
|
|
static uint64_t getRelocTargetVA(const InputFile *file, RelType type, int64_t a,
|
2018-06-11 07:24:31 +00:00
|
|
|
uint64_t p, const Symbol &sym, RelExpr expr) {
|
2016-04-13 01:40:19 +00:00
|
|
|
switch (expr) {
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_ABS:
|
2019-04-23 06:31:44 +00:00
|
|
|
case R_DTPREL:
|
2018-07-09 16:35:51 +00:00
|
|
|
case R_RELAX_TLS_LD_TO_LE_ABS:
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_RELAX_GOT_PC_NOPIC:
|
[ELF][RISCV] Treat R_RISCV_{ADD,SET,SUB}* as link-time constants
R_RISCV_{ADD,SET,SUB}* are used for local label computation.
Add a new RelExpr member R_RISCV_ADD to represent them.
R_RISCV_ADD is treated as a link-time constant because otherwise
R_RISCV_{ADD,SET,SUB}* are not allowed in -pie/-shared mode.
In glibc Scrt1.o, .rela.eh_frame contains such relocations.
Because .eh_frame is not writable, we get this error:
ld.lld: error: can't create dynamic relocation R_RISCV_ADD32 against symbol: .L0 in readonly segment; recompil object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
>>> defined in ..../riscv64-linux-gnu/lib/Scrt1.o
With D63076 and this patch, I can run -pie/-shared programs linked against glibc.
Note llvm-mc cannot currently produce R_RISCV_SET* so they are not tested.
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D63183
llvm-svn: 363128
2019-06-12 07:53:06 +00:00
|
|
|
case R_RISCV_ADD:
|
2017-11-04 00:31:04 +00:00
|
|
|
return sym.getVA(a);
|
2018-02-16 10:01:17 +00:00
|
|
|
case R_ADDEND:
|
|
|
|
return a;
|
2017-05-18 09:12:21 +00:00
|
|
|
case R_ARM_SBREL:
|
2017-11-04 00:31:04 +00:00
|
|
|
return sym.getVA(a) - getARMStaticBase(sym);
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_GOT:
|
|
|
|
case R_RELAX_TLS_GD_TO_IE_ABS:
|
2017-11-04 00:31:04 +00:00
|
|
|
return sym.getGotVA() + a;
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_GOTONLY_PC:
|
2018-09-25 19:26:58 +00:00
|
|
|
return in.got->getVA() + a - p;
|
[ELF] Change GOT*_FROM_END (relative to end(.got)) to GOTPLT* (start(.got.plt))
Summary:
This should address remaining issues discussed in PR36555.
Currently R_GOT*_FROM_END are exclusively used by x86 and x86_64 to
express relocations types relative to the GOT base. We have
_GLOBAL_OFFSET_TABLE_ (GOT base) = start(.got.plt) but end(.got) !=
start(.got.plt)
This can have problems when _GLOBAL_OFFSET_TABLE_ is used as a symbol, e.g.
glibc dl_machine_dynamic assumes _GLOBAL_OFFSET_TABLE_ is start(.got.plt),
which is not true.
extern const ElfW(Addr) _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
return _GLOBAL_OFFSET_TABLE_[0]; // R_X86_64_GOTPC32
In this patch, we
* Change all GOT*_FROM_END to GOTPLT* to fix the problem.
* Add HasGotPltOffRel to denote whether .got.plt should be kept even if
the section is empty.
* Simplify GotSection::empty and GotPltSection::empty by setting
HasGotOffRel and HasGotPltOffRel according to GlobalOffsetTable early.
The change of R_386_GOTPC makes X86::writePltHeader simpler as we don't
have to compute the offset start(.got.plt) - Ebx (it is constant 0).
We still diverge from ld.bfd (at least in most cases) and gold in that
.got.plt and .got are not adjacent, but the advantage doing that is
unclear.
Reviewers: ruiu, sivachandra, espindola
Subscribers: emaste, mehdi_amini, arichardson, dexonsmith, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59594
llvm-svn: 356968
2019-03-25 23:46:19 +00:00
|
|
|
case R_GOTPLTONLY_PC:
|
|
|
|
return in.gotPlt->getVA() + a - p;
|
2016-04-18 12:07:13 +00:00
|
|
|
case R_GOTREL:
|
[PPC64] toc-indirect to toc-relative relaxation
This is based on D54720 by Sean Fertile.
When accessing a global symbol which is not defined in the translation unit,
compilers will generate instructions that load the address from the toc entry.
If the symbol is defined, non-preemptable, and addressable with a 32-bit
signed offset from the toc pointer, the address can be computed
directly. e.g.
addis 3, 2, .LC0@toc@ha # R_PPC64_TOC16_HA
ld 3, .LC0@toc@l(3) # R_PPC64_TOC16_LO_DS, load the address from a .toc entry
ld/lwa 3, 0(3) # load the value from the address
.section .toc,"aw",@progbits
.LC0: .tc var[TC],var
can be relaxed to
addis 3,2,var@toc@ha # this may be relaxed to a nop,
addi 3,3,var@toc@l # then this becomes addi 3,2,var@toc
ld/lwa 3, 0(3) # load the value from the address
We can delete the test ppc64-got-indirect.s as its purpose is covered by
newly added ppc64-toc-relax.s and ppc64-toc-relax-constants.s
Reviewed By: ruiu, sfertile
Differential Revision: https://reviews.llvm.org/D60958
llvm-svn: 360112
2019-05-07 04:26:05 +00:00
|
|
|
case R_PPC64_RELAX_TOC:
|
2018-09-25 19:26:58 +00:00
|
|
|
return sym.getVA(a) - in.got->getVA();
|
[ELF] Change GOT*_FROM_END (relative to end(.got)) to GOTPLT* (start(.got.plt))
Summary:
This should address remaining issues discussed in PR36555.
Currently R_GOT*_FROM_END are exclusively used by x86 and x86_64 to
express relocations types relative to the GOT base. We have
_GLOBAL_OFFSET_TABLE_ (GOT base) = start(.got.plt) but end(.got) !=
start(.got.plt)
This can have problems when _GLOBAL_OFFSET_TABLE_ is used as a symbol, e.g.
glibc dl_machine_dynamic assumes _GLOBAL_OFFSET_TABLE_ is start(.got.plt),
which is not true.
extern const ElfW(Addr) _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
return _GLOBAL_OFFSET_TABLE_[0]; // R_X86_64_GOTPC32
In this patch, we
* Change all GOT*_FROM_END to GOTPLT* to fix the problem.
* Add HasGotPltOffRel to denote whether .got.plt should be kept even if
the section is empty.
* Simplify GotSection::empty and GotPltSection::empty by setting
HasGotOffRel and HasGotPltOffRel according to GlobalOffsetTable early.
The change of R_386_GOTPC makes X86::writePltHeader simpler as we don't
have to compute the offset start(.got.plt) - Ebx (it is constant 0).
We still diverge from ld.bfd (at least in most cases) and gold in that
.got.plt and .got are not adjacent, but the advantage doing that is
unclear.
Reviewers: ruiu, sivachandra, espindola
Subscribers: emaste, mehdi_amini, arichardson, dexonsmith, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59594
llvm-svn: 356968
2019-03-25 23:46:19 +00:00
|
|
|
case R_GOTPLTREL:
|
|
|
|
return sym.getVA(a) - in.gotPlt->getVA();
|
|
|
|
case R_GOTPLT:
|
2019-04-22 02:48:37 +00:00
|
|
|
case R_RELAX_TLS_GD_TO_IE_GOTPLT:
|
[ELF] Change GOT*_FROM_END (relative to end(.got)) to GOTPLT* (start(.got.plt))
Summary:
This should address remaining issues discussed in PR36555.
Currently R_GOT*_FROM_END are exclusively used by x86 and x86_64 to
express relocations types relative to the GOT base. We have
_GLOBAL_OFFSET_TABLE_ (GOT base) = start(.got.plt) but end(.got) !=
start(.got.plt)
This can have problems when _GLOBAL_OFFSET_TABLE_ is used as a symbol, e.g.
glibc dl_machine_dynamic assumes _GLOBAL_OFFSET_TABLE_ is start(.got.plt),
which is not true.
extern const ElfW(Addr) _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
return _GLOBAL_OFFSET_TABLE_[0]; // R_X86_64_GOTPC32
In this patch, we
* Change all GOT*_FROM_END to GOTPLT* to fix the problem.
* Add HasGotPltOffRel to denote whether .got.plt should be kept even if
the section is empty.
* Simplify GotSection::empty and GotPltSection::empty by setting
HasGotOffRel and HasGotPltOffRel according to GlobalOffsetTable early.
The change of R_386_GOTPC makes X86::writePltHeader simpler as we don't
have to compute the offset start(.got.plt) - Ebx (it is constant 0).
We still diverge from ld.bfd (at least in most cases) and gold in that
.got.plt and .got are not adjacent, but the advantage doing that is
unclear.
Reviewers: ruiu, sivachandra, espindola
Subscribers: emaste, mehdi_amini, arichardson, dexonsmith, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59594
llvm-svn: 356968
2019-03-25 23:46:19 +00:00
|
|
|
return sym.getGotVA() + a - in.gotPlt->getVA();
|
2018-06-27 13:55:41 +00:00
|
|
|
case R_TLSLD_GOT_OFF:
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_GOT_OFF:
|
[PPC64] Thread-local storage general-dynamic to initial-exec relaxation.
Patch adds support for relaxing the general-dynamic tls sequence to
initial-exec.
the relaxation performs the following transformation:
addis r3, r2, x@got@tlsgd@ha --> addis r3, r2, x@got@tprel@ha
addi r3, r3, x@got@tlsgd@l --> ld r3, x@got@tprel@l(r3)
bl __tls_get_addr(x@tlsgd) --> nop
nop --> add r3, r3, r13
and instead of emitting a DTPMOD64/DTPREL64 pair for x, we emit a single
R_PPC64_TPREL64.
Differential Revision: https://reviews.llvm.org/D48090
llvm-svn: 335651
2018-06-26 19:38:18 +00:00
|
|
|
case R_RELAX_TLS_GD_TO_IE_GOT_OFF:
|
2017-11-04 00:31:04 +00:00
|
|
|
return sym.getGotOffset() + a;
|
2018-11-13 10:16:36 +00:00
|
|
|
case R_AARCH64_GOT_PAGE_PC:
|
|
|
|
case R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC:
|
2017-11-04 00:31:04 +00:00
|
|
|
return getAArch64Page(sym.getGotVA() + a) - getAArch64Page(p);
|
2016-04-13 01:40:19 +00:00
|
|
|
case R_GOT_PC:
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_RELAX_TLS_GD_TO_IE:
|
2017-11-04 00:31:04 +00:00
|
|
|
return sym.getGotVA() + a - p;
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_MIPS_GOTREL:
|
2018-09-25 19:26:58 +00:00
|
|
|
return sym.getVA(a) - in.mipsGot->getGp(file);
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_MIPS_GOT_GP:
|
2018-09-25 19:26:58 +00:00
|
|
|
return in.mipsGot->getGp(file) + a;
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_MIPS_GOT_GP_PC: {
|
|
|
|
// R_MIPS_LO16 expression has R_MIPS_GOT_GP_PC type iif the target
|
|
|
|
// is _gp_disp symbol. In that case we should use the following
|
|
|
|
// formula for calculation "AHL + GP - P + 4". For details see p. 4-19 at
|
|
|
|
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
|
2017-11-09 12:10:14 +00:00
|
|
|
// microMIPS variants of these relocations use slightly different
|
|
|
|
// expressions: AHL + GP - P + 3 for %lo() and AHL + GP - P - 1 for %hi()
|
|
|
|
// to correctly handle less-sugnificant bit of the microMIPS symbol.
|
2018-09-25 19:26:58 +00:00
|
|
|
uint64_t v = in.mipsGot->getGp(file) + a - p;
|
2017-09-12 13:08:24 +00:00
|
|
|
if (type == R_MIPS_LO16 || type == R_MICROMIPS_LO16)
|
2017-03-26 04:10:43 +00:00
|
|
|
v += 4;
|
2017-11-09 12:10:14 +00:00
|
|
|
if (type == R_MICROMIPS_LO16 || type == R_MICROMIPS_HI16)
|
|
|
|
v -= 1;
|
2017-03-26 04:10:43 +00:00
|
|
|
return v;
|
|
|
|
}
|
2016-05-15 18:13:50 +00:00
|
|
|
case R_MIPS_GOT_LOCAL_PAGE:
|
2016-03-13 15:37:38 +00:00
|
|
|
// If relocation against MIPS local symbol requires GOT entry, this entry
|
|
|
|
// should be initialized by 'page address'. This address is high 16-bits
|
2016-03-30 12:45:58 +00:00
|
|
|
// of sum the symbol's value and the addend.
|
2018-09-25 19:26:58 +00:00
|
|
|
return in.mipsGot->getVA() + in.mipsGot->getPageEntryOffset(file, sym, a) -
|
|
|
|
in.mipsGot->getGp(file);
|
2016-06-19 21:39:37 +00:00
|
|
|
case R_MIPS_GOT_OFF:
|
2016-10-21 07:22:30 +00:00
|
|
|
case R_MIPS_GOT_OFF32:
|
2016-06-19 21:39:37 +00:00
|
|
|
// In case of MIPS if a GOT relocation has non-zero addend this addend
|
|
|
|
// should be applied to the GOT entry content not to the GOT entry offset.
|
|
|
|
// That is why we use separate expression type.
|
2018-09-25 19:26:58 +00:00
|
|
|
return in.mipsGot->getVA() + in.mipsGot->getSymEntryOffset(file, sym, a) -
|
|
|
|
in.mipsGot->getGp(file);
|
2016-06-23 15:26:31 +00:00
|
|
|
case R_MIPS_TLSGD:
|
2018-09-25 19:26:58 +00:00
|
|
|
return in.mipsGot->getVA() + in.mipsGot->getGlobalDynOffset(file, sym) -
|
|
|
|
in.mipsGot->getGp(file);
|
2016-06-23 15:26:31 +00:00
|
|
|
case R_MIPS_TLSLD:
|
2018-09-25 19:26:58 +00:00
|
|
|
return in.mipsGot->getVA() + in.mipsGot->getTlsIndexOffset(file) -
|
|
|
|
in.mipsGot->getGp(file);
|
2018-11-15 15:35:44 +00:00
|
|
|
case R_AARCH64_PAGE_PC: {
|
2018-12-13 11:13:01 +00:00
|
|
|
uint64_t val = sym.isUndefWeak() ? p + a : sym.getVA(a);
|
2018-11-14 13:53:47 +00:00
|
|
|
return getAArch64Page(val) - getAArch64Page(p);
|
|
|
|
}
|
2018-08-09 17:59:56 +00:00
|
|
|
case R_RISCV_PC_INDIRECT: {
|
2018-11-23 15:13:26 +00:00
|
|
|
if (const Relocation *hiRel = getRISCVPCRelHi20(&sym, a))
|
|
|
|
return getRelocTargetVA(file, hiRel->type, hiRel->addend, sym.getVA(),
|
|
|
|
*hiRel->sym, hiRel->expr);
|
|
|
|
return 0;
|
2018-08-09 17:59:56 +00:00
|
|
|
}
|
2017-06-12 18:05:01 +00:00
|
|
|
case R_PC: {
|
|
|
|
uint64_t dest;
|
2017-11-04 00:31:04 +00:00
|
|
|
if (sym.isUndefWeak()) {
|
2017-03-26 04:10:43 +00:00
|
|
|
// On ARM and AArch64 a branch to an undefined weak resolves to the
|
|
|
|
// next instruction, otherwise the place.
|
|
|
|
if (config->emachine == EM_ARM)
|
2017-06-12 18:05:01 +00:00
|
|
|
dest = getARMUndefinedRelativeWeakVA(type, a, p);
|
|
|
|
else if (config->emachine == EM_AARCH64)
|
|
|
|
dest = getAArch64UndefinedRelativeWeakVA(type, a, p);
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-06 17:03:00 +00:00
|
|
|
else if (config->emachine == EM_PPC)
|
|
|
|
dest = p;
|
2017-06-12 18:05:01 +00:00
|
|
|
else
|
2017-11-04 00:31:04 +00:00
|
|
|
dest = sym.getVA(a);
|
2017-06-12 18:05:01 +00:00
|
|
|
} else {
|
2017-11-04 00:31:04 +00:00
|
|
|
dest = sym.getVA(a);
|
2017-03-26 04:10:43 +00:00
|
|
|
}
|
2017-06-12 18:05:01 +00:00
|
|
|
return dest - p;
|
|
|
|
}
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_PLT:
|
2017-11-04 00:31:04 +00:00
|
|
|
return sym.getPltVA() + a;
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_PLT_PC:
|
2019-06-03 06:21:33 +00:00
|
|
|
case R_PPC64_CALL_PLT:
|
2017-11-04 00:31:04 +00:00
|
|
|
return sym.getPltVA() + a - p;
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-06 17:03:00 +00:00
|
|
|
case R_PPC32_PLTREL:
|
|
|
|
// R_PPC_PLTREL24 uses the addend (usually 0 or 0x8000) to indicate r30
|
|
|
|
// stores _GLOBAL_OFFSET_TABLE_ or .got2+0x8000. The addend is ignored for
|
2019-10-29 01:41:38 +00:00
|
|
|
// target VA computation.
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-06 17:03:00 +00:00
|
|
|
return sym.getPltVA() - p;
|
2019-06-03 06:21:33 +00:00
|
|
|
case R_PPC64_CALL: {
|
2017-11-04 00:31:04 +00:00
|
|
|
uint64_t symVA = sym.getVA(a);
|
2016-04-13 01:40:19 +00:00
|
|
|
// If we have an undefined weak symbol, we might get here with a symbol
|
|
|
|
// address of zero. That could overflow, but the code must be unreachable,
|
|
|
|
// so don't bother doing anything at all.
|
|
|
|
if (!symVA)
|
|
|
|
return 0;
|
2018-04-27 15:41:19 +00:00
|
|
|
|
|
|
|
// PPC64 V2 ABI describes two entry points to a function. The global entry
|
2018-09-20 00:26:47 +00:00
|
|
|
// point is used for calls where the caller and callee (may) have different
|
|
|
|
// TOC base pointers and r2 needs to be modified to hold the TOC base for
|
|
|
|
// the callee. For local calls the caller and callee share the same
|
|
|
|
// TOC base and so the TOC pointer initialization code should be skipped by
|
|
|
|
// branching to the local entry point.
|
|
|
|
return symVA - p + getPPC64GlobalEntryToLocalEntryOffset(sym.stOther);
|
2016-04-13 01:40:19 +00:00
|
|
|
}
|
2019-06-03 06:21:33 +00:00
|
|
|
case R_PPC64_TOCBASE:
|
2017-03-26 04:10:43 +00:00
|
|
|
return getPPC64TocBase() + a;
|
2016-05-25 14:31:37 +00:00
|
|
|
case R_RELAX_GOT_PC:
|
2017-11-04 00:31:04 +00:00
|
|
|
return sym.getVA(a) - p;
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_RELAX_TLS_GD_TO_LE:
|
|
|
|
case R_RELAX_TLS_IE_TO_LE:
|
|
|
|
case R_RELAX_TLS_LD_TO_LE:
|
|
|
|
case R_TLS:
|
2019-05-20 11:47:31 +00:00
|
|
|
// It is not very clear what to return if the symbol is undefined. With
|
|
|
|
// --noinhibit-exec, even a non-weak undefined reference may reach here.
|
|
|
|
// Just return A, which matches R_ABS, and the behavior of some dynamic
|
|
|
|
// loaders.
|
|
|
|
if (sym.isUndefined())
|
|
|
|
return a;
|
2019-05-30 10:00:20 +00:00
|
|
|
return getTlsTpOffset(sym) + a;
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_RELAX_TLS_GD_TO_LE_NEG:
|
|
|
|
case R_NEG_TLS:
|
2019-05-20 11:47:31 +00:00
|
|
|
if (sym.isUndefined())
|
|
|
|
return a;
|
2019-05-30 10:00:20 +00:00
|
|
|
return -getTlsTpOffset(sym) + a;
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_SIZE:
|
2018-01-05 21:41:17 +00:00
|
|
|
return sym.getSize() + a;
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_TLSDESC:
|
2018-09-25 19:26:58 +00:00
|
|
|
return in.got->getGlobalDynAddr(sym) + a;
|
2019-05-29 02:03:56 +00:00
|
|
|
case R_TLSDESC_PC:
|
|
|
|
return in.got->getGlobalDynAddr(sym) + a - p;
|
2018-11-15 15:35:44 +00:00
|
|
|
case R_AARCH64_TLSDESC_PAGE:
|
2018-09-25 19:26:58 +00:00
|
|
|
return getAArch64Page(in.got->getGlobalDynAddr(sym) + a) -
|
2017-03-26 04:10:43 +00:00
|
|
|
getAArch64Page(p);
|
2018-05-29 14:34:38 +00:00
|
|
|
case R_TLSGD_GOT:
|
2018-09-25 19:26:58 +00:00
|
|
|
return in.got->getGlobalDynOffset(sym) + a;
|
[ELF] Change GOT*_FROM_END (relative to end(.got)) to GOTPLT* (start(.got.plt))
Summary:
This should address remaining issues discussed in PR36555.
Currently R_GOT*_FROM_END are exclusively used by x86 and x86_64 to
express relocations types relative to the GOT base. We have
_GLOBAL_OFFSET_TABLE_ (GOT base) = start(.got.plt) but end(.got) !=
start(.got.plt)
This can have problems when _GLOBAL_OFFSET_TABLE_ is used as a symbol, e.g.
glibc dl_machine_dynamic assumes _GLOBAL_OFFSET_TABLE_ is start(.got.plt),
which is not true.
extern const ElfW(Addr) _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
return _GLOBAL_OFFSET_TABLE_[0]; // R_X86_64_GOTPC32
In this patch, we
* Change all GOT*_FROM_END to GOTPLT* to fix the problem.
* Add HasGotPltOffRel to denote whether .got.plt should be kept even if
the section is empty.
* Simplify GotSection::empty and GotPltSection::empty by setting
HasGotOffRel and HasGotPltOffRel according to GlobalOffsetTable early.
The change of R_386_GOTPC makes X86::writePltHeader simpler as we don't
have to compute the offset start(.got.plt) - Ebx (it is constant 0).
We still diverge from ld.bfd (at least in most cases) and gold in that
.got.plt and .got are not adjacent, but the advantage doing that is
unclear.
Reviewers: ruiu, sivachandra, espindola
Subscribers: emaste, mehdi_amini, arichardson, dexonsmith, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59594
llvm-svn: 356968
2019-03-25 23:46:19 +00:00
|
|
|
case R_TLSGD_GOTPLT:
|
|
|
|
return in.got->getVA() + in.got->getGlobalDynOffset(sym) + a - in.gotPlt->getVA();
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_TLSGD_PC:
|
2018-09-25 19:26:58 +00:00
|
|
|
return in.got->getGlobalDynAddr(sym) + a - p;
|
[ELF] Change GOT*_FROM_END (relative to end(.got)) to GOTPLT* (start(.got.plt))
Summary:
This should address remaining issues discussed in PR36555.
Currently R_GOT*_FROM_END are exclusively used by x86 and x86_64 to
express relocations types relative to the GOT base. We have
_GLOBAL_OFFSET_TABLE_ (GOT base) = start(.got.plt) but end(.got) !=
start(.got.plt)
This can have problems when _GLOBAL_OFFSET_TABLE_ is used as a symbol, e.g.
glibc dl_machine_dynamic assumes _GLOBAL_OFFSET_TABLE_ is start(.got.plt),
which is not true.
extern const ElfW(Addr) _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
return _GLOBAL_OFFSET_TABLE_[0]; // R_X86_64_GOTPC32
In this patch, we
* Change all GOT*_FROM_END to GOTPLT* to fix the problem.
* Add HasGotPltOffRel to denote whether .got.plt should be kept even if
the section is empty.
* Simplify GotSection::empty and GotPltSection::empty by setting
HasGotOffRel and HasGotPltOffRel according to GlobalOffsetTable early.
The change of R_386_GOTPC makes X86::writePltHeader simpler as we don't
have to compute the offset start(.got.plt) - Ebx (it is constant 0).
We still diverge from ld.bfd (at least in most cases) and gold in that
.got.plt and .got are not adjacent, but the advantage doing that is
unclear.
Reviewers: ruiu, sivachandra, espindola
Subscribers: emaste, mehdi_amini, arichardson, dexonsmith, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59594
llvm-svn: 356968
2019-03-25 23:46:19 +00:00
|
|
|
case R_TLSLD_GOTPLT:
|
|
|
|
return in.got->getVA() + in.got->getTlsIndexOff() + a - in.gotPlt->getVA();
|
2018-05-31 18:44:12 +00:00
|
|
|
case R_TLSLD_GOT:
|
2018-09-25 19:26:58 +00:00
|
|
|
return in.got->getTlsIndexOff() + a;
|
2017-03-26 04:10:43 +00:00
|
|
|
case R_TLSLD_PC:
|
2018-09-25 19:26:58 +00:00
|
|
|
return in.got->getTlsIndexVA() + a - p;
|
2018-08-02 14:34:39 +00:00
|
|
|
default:
|
|
|
|
llvm_unreachable("invalid expression");
|
2016-04-13 01:40:19 +00:00
|
|
|
}
|
2016-03-13 15:37:38 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 18:42:04 +00:00
|
|
|
// This function applies relocations to sections without SHF_ALLOC bit.
|
|
|
|
// Such sections are never mapped to memory at runtime. Debug sections are
|
|
|
|
// an example. Relocations in non-alloc sections are much easier to
|
|
|
|
// handle than in allocated sections because it will never need complex
|
2019-10-31 11:54:16 +00:00
|
|
|
// treatment such as GOT or PLT (because at runtime no one refers them).
|
2016-04-28 18:42:04 +00:00
|
|
|
// So, we handle relocations for non-alloc sections directly in this
|
|
|
|
// function as a performance optimization.
|
2017-02-23 16:49:07 +00:00
|
|
|
template <class ELFT, class RelTy>
|
|
|
|
void InputSection::relocateNonAlloc(uint8_t *buf, ArrayRef<RelTy> rels) {
|
2017-10-10 04:45:48 +00:00
|
|
|
const unsigned bits = sizeof(typename ELFT::uint) * 8;
|
|
|
|
|
2016-04-28 18:42:04 +00:00
|
|
|
for (const RelTy &rel : rels) {
|
2017-10-11 22:49:24 +00:00
|
|
|
RelType type = rel.getType(config->isMips64EL);
|
2018-02-16 01:10:51 +00:00
|
|
|
|
|
|
|
// GCC 8.0 or earlier have a bug that they emit R_386_GOTPC relocations
|
|
|
|
// against _GLOBAL_OFFSET_TABLE_ for .debug_info. The bug has been fixed
|
|
|
|
// in 2017 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630), but we
|
|
|
|
// need to keep this bug-compatible code for a while.
|
|
|
|
if (config->emachine == EM_386 && type == R_386_GOTPC)
|
|
|
|
continue;
|
|
|
|
|
2017-03-08 16:03:41 +00:00
|
|
|
uint64_t offset = getOffset(rel.r_offset);
|
2016-05-21 19:48:54 +00:00
|
|
|
uint8_t *bufLoc = buf + offset;
|
2017-02-16 00:12:34 +00:00
|
|
|
int64_t addend = getAddend<ELFT>(rel);
|
2016-04-29 03:21:08 +00:00
|
|
|
if (!RelTy::IsRela)
|
2016-05-21 19:48:54 +00:00
|
|
|
addend += target->getImplicitAddend(bufLoc, type);
|
2016-04-28 18:42:04 +00:00
|
|
|
|
2017-12-19 16:29:02 +00:00
|
|
|
Symbol &sym = getFile<ELFT>()->getRelocTargetSym(rel);
|
2017-10-12 03:14:06 +00:00
|
|
|
RelExpr expr = target->getRelExpr(type, sym, bufLoc);
|
2017-02-23 06:22:28 +00:00
|
|
|
if (expr == R_NONE)
|
|
|
|
continue;
|
2018-02-16 01:10:51 +00:00
|
|
|
|
2019-07-03 12:48:04 +00:00
|
|
|
if (expr != R_ABS && expr != R_DTPREL && expr != R_RISCV_ADD) {
|
2018-02-16 01:10:51 +00:00
|
|
|
std::string msg = getLocation<ELFT>(offset) +
|
|
|
|
": has non-ABS relocation " + toString(type) +
|
|
|
|
" against symbol '" + toString(sym) + "'";
|
|
|
|
if (expr != R_PC) {
|
|
|
|
error(msg);
|
|
|
|
return;
|
|
|
|
}
|
2017-10-27 11:49:24 +00:00
|
|
|
|
2018-02-16 01:10:51 +00:00
|
|
|
// If the control reaches here, we found a PC-relative relocation in a
|
|
|
|
// non-ALLOC section. Since non-ALLOC section is not loaded into memory
|
|
|
|
// at runtime, the notion of PC-relative doesn't make sense here. So,
|
|
|
|
// this is a usage error. However, GNU linkers historically accept such
|
|
|
|
// relocations without any errors and relocate them as if they were at
|
|
|
|
// address 0. For bug-compatibilty, we accept them with warnings. We
|
|
|
|
// know Steel Bank Common Lisp as of 2018 have this bug.
|
|
|
|
warn(msg);
|
|
|
|
target->relocateOne(bufLoc, type,
|
|
|
|
SignExtend64<bits>(sym.getVA(addend - offset)));
|
|
|
|
continue;
|
2016-04-28 18:42:04 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 04:45:48 +00:00
|
|
|
if (sym.isTls() && !Out::tlsPhdr)
|
|
|
|
target->relocateOne(bufLoc, type, 0);
|
|
|
|
else
|
|
|
|
target->relocateOne(bufLoc, type, SignExtend64<bits>(sym.getVA(addend)));
|
2016-04-28 18:42:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 12:52:04 +00:00
|
|
|
// This is used when '-r' is given.
|
|
|
|
// For REL targets, InputSection::copyRelocations() may store artificial
|
|
|
|
// relocations aimed to update addends. They are handled in relocateAlloc()
|
|
|
|
// for allocatable sections, and this function does the same for
|
|
|
|
// non-allocatable sections, such as sections with debug information.
|
|
|
|
static void relocateNonAllocForRelocatable(InputSection *sec, uint8_t *buf) {
|
|
|
|
const unsigned bits = config->is64 ? 64 : 32;
|
|
|
|
|
|
|
|
for (const Relocation &rel : sec->relocations) {
|
|
|
|
// InputSection::copyRelocations() adds only R_ABS relocations.
|
|
|
|
assert(rel.expr == R_ABS);
|
|
|
|
uint8_t *bufLoc = buf + rel.offset + sec->outSecOff;
|
|
|
|
uint64_t targetVA = SignExtend64(rel.sym->getVA(rel.addend), bits);
|
|
|
|
target->relocateOne(bufLoc, rel.type, targetVA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 18:11:26 +00:00
|
|
|
template <class ELFT>
|
2017-02-23 02:28:28 +00:00
|
|
|
void InputSectionBase::relocate(uint8_t *buf, uint8_t *bufEnd) {
|
2018-07-17 23:16:02 +00:00
|
|
|
if (flags & SHF_EXECINSTR)
|
|
|
|
adjustSplitStackFunctionPrologues<ELFT>(buf, bufEnd);
|
|
|
|
|
2017-10-06 20:08:51 +00:00
|
|
|
if (flags & SHF_ALLOC) {
|
2017-05-18 16:45:36 +00:00
|
|
|
relocateAlloc(buf, bufEnd);
|
2017-10-06 20:08:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-05-18 16:45:36 +00:00
|
|
|
|
2017-10-06 20:08:51 +00:00
|
|
|
auto *sec = cast<InputSection>(this);
|
2018-07-11 12:52:04 +00:00
|
|
|
if (config->relocatable)
|
|
|
|
relocateNonAllocForRelocatable(sec, buf);
|
|
|
|
else if (sec->areRelocsRela)
|
2017-10-06 20:08:51 +00:00
|
|
|
sec->relocateNonAlloc<ELFT>(buf, sec->template relas<ELFT>());
|
2017-05-18 16:45:36 +00:00
|
|
|
else
|
2017-10-06 20:08:51 +00:00
|
|
|
sec->relocateNonAlloc<ELFT>(buf, sec->template rels<ELFT>());
|
2017-05-18 16:45:36 +00:00
|
|
|
}
|
2016-04-28 18:42:04 +00:00
|
|
|
|
2017-05-18 16:45:36 +00:00
|
|
|
void InputSectionBase::relocateAlloc(uint8_t *buf, uint8_t *bufEnd) {
|
|
|
|
assert(flags & SHF_ALLOC);
|
|
|
|
const unsigned bits = config->wordsize * 8;
|
2017-10-06 20:08:51 +00:00
|
|
|
|
2016-09-07 20:37:34 +00:00
|
|
|
for (const Relocation &rel : relocations) {
|
2018-04-19 03:51:26 +00:00
|
|
|
uint64_t offset = rel.offset;
|
|
|
|
if (auto *sec = dyn_cast<InputSection>(this))
|
|
|
|
offset += sec->outSecOff;
|
2018-04-19 02:24:28 +00:00
|
|
|
uint8_t *bufLoc = buf + offset;
|
2017-10-11 22:49:24 +00:00
|
|
|
RelType type = rel.type;
|
2016-04-13 01:40:19 +00:00
|
|
|
|
2018-04-19 02:24:28 +00:00
|
|
|
uint64_t addrLoc = getOutputSection()->addr + offset;
|
2016-04-13 01:40:19 +00:00
|
|
|
RelExpr expr = rel.expr;
|
2017-05-18 16:45:36 +00:00
|
|
|
uint64_t targetVA = SignExtend64(
|
2018-06-11 08:37:19 +00:00
|
|
|
getRelocTargetVA(file, type, rel.addend, addrLoc, *rel.sym, expr),
|
2018-06-11 07:24:31 +00:00
|
|
|
bits);
|
2016-03-18 18:11:26 +00:00
|
|
|
|
2016-05-20 21:14:06 +00:00
|
|
|
switch (expr) {
|
2016-05-25 14:31:37 +00:00
|
|
|
case R_RELAX_GOT_PC:
|
[ELF] - Implemented support for test/binop relaxations from latest ABI.
Patch implements next relaxation from latest ABI:
"Convert memory operand of test and binop into immediate operand, where binop is one of adc, add, and, cmp, or,
sbb, sub, xor instructions, when position-independent code is disabled."
It is described in System V Application Binary Interface AMD64 Architecture Processor
Supplement Draft Version 0.99.8 (https://github.com/hjl-tools/x86-psABI/wiki/x86-64-psABI-r249.pdf,
B.2 "B.2 Optimize GOTPCRELX Relocations").
Differential revision: http://reviews.llvm.org/D20793
llvm-svn: 271405
2016-06-01 16:45:30 +00:00
|
|
|
case R_RELAX_GOT_PC_NOPIC:
|
[PPC64] toc-indirect to toc-relative relaxation
This is based on D54720 by Sean Fertile.
When accessing a global symbol which is not defined in the translation unit,
compilers will generate instructions that load the address from the toc entry.
If the symbol is defined, non-preemptable, and addressable with a 32-bit
signed offset from the toc pointer, the address can be computed
directly. e.g.
addis 3, 2, .LC0@toc@ha # R_PPC64_TOC16_HA
ld 3, .LC0@toc@l(3) # R_PPC64_TOC16_LO_DS, load the address from a .toc entry
ld/lwa 3, 0(3) # load the value from the address
.section .toc,"aw",@progbits
.LC0: .tc var[TC],var
can be relaxed to
addis 3,2,var@toc@ha # this may be relaxed to a nop,
addi 3,3,var@toc@l # then this becomes addi 3,2,var@toc
ld/lwa 3, 0(3) # load the value from the address
We can delete the test ppc64-got-indirect.s as its purpose is covered by
newly added ppc64-toc-relax.s and ppc64-toc-relax-constants.s
Reviewed By: ruiu, sfertile
Differential Revision: https://reviews.llvm.org/D60958
llvm-svn: 360112
2019-05-07 04:26:05 +00:00
|
|
|
target->relaxGot(bufLoc, type, targetVA);
|
|
|
|
break;
|
|
|
|
case R_PPC64_RELAX_TOC:
|
|
|
|
if (!tryRelaxPPC64TocIndirection(type, rel, bufLoc))
|
|
|
|
target->relocateOne(bufLoc, type, targetVA);
|
2016-05-25 14:31:37 +00:00
|
|
|
break;
|
2016-05-20 21:14:06 +00:00
|
|
|
case R_RELAX_TLS_IE_TO_LE:
|
2016-12-14 22:45:52 +00:00
|
|
|
target->relaxTlsIeToLe(bufLoc, type, targetVA);
|
2016-05-20 21:14:06 +00:00
|
|
|
break;
|
|
|
|
case R_RELAX_TLS_LD_TO_LE:
|
2018-07-09 16:35:51 +00:00
|
|
|
case R_RELAX_TLS_LD_TO_LE_ABS:
|
2016-12-14 22:45:52 +00:00
|
|
|
target->relaxTlsLdToLe(bufLoc, type, targetVA);
|
2016-05-20 21:14:06 +00:00
|
|
|
break;
|
|
|
|
case R_RELAX_TLS_GD_TO_LE:
|
2016-06-04 23:22:34 +00:00
|
|
|
case R_RELAX_TLS_GD_TO_LE_NEG:
|
2016-12-14 22:45:52 +00:00
|
|
|
target->relaxTlsGdToLe(bufLoc, type, targetVA);
|
2016-05-20 21:14:06 +00:00
|
|
|
break;
|
2018-11-13 10:16:36 +00:00
|
|
|
case R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC:
|
2016-05-20 21:14:06 +00:00
|
|
|
case R_RELAX_TLS_GD_TO_IE:
|
2016-06-04 23:33:31 +00:00
|
|
|
case R_RELAX_TLS_GD_TO_IE_ABS:
|
[PPC64] Thread-local storage general-dynamic to initial-exec relaxation.
Patch adds support for relaxing the general-dynamic tls sequence to
initial-exec.
the relaxation performs the following transformation:
addis r3, r2, x@got@tlsgd@ha --> addis r3, r2, x@got@tprel@ha
addi r3, r3, x@got@tlsgd@l --> ld r3, x@got@tprel@l(r3)
bl __tls_get_addr(x@tlsgd) --> nop
nop --> add r3, r3, r13
and instead of emitting a DTPMOD64/DTPREL64 pair for x, we emit a single
R_PPC64_TPREL64.
Differential Revision: https://reviews.llvm.org/D48090
llvm-svn: 335651
2018-06-26 19:38:18 +00:00
|
|
|
case R_RELAX_TLS_GD_TO_IE_GOT_OFF:
|
2019-04-22 02:48:37 +00:00
|
|
|
case R_RELAX_TLS_GD_TO_IE_GOTPLT:
|
2016-12-14 22:45:52 +00:00
|
|
|
target->relaxTlsGdToIe(bufLoc, type, targetVA);
|
2016-05-20 21:14:06 +00:00
|
|
|
break;
|
2019-06-03 06:21:33 +00:00
|
|
|
case R_PPC64_CALL:
|
[PPC64] Thread-local storage general-dynamic to initial-exec relaxation.
Patch adds support for relaxing the general-dynamic tls sequence to
initial-exec.
the relaxation performs the following transformation:
addis r3, r2, x@got@tlsgd@ha --> addis r3, r2, x@got@tprel@ha
addi r3, r3, x@got@tlsgd@l --> ld r3, x@got@tprel@l(r3)
bl __tls_get_addr(x@tlsgd) --> nop
nop --> add r3, r3, r13
and instead of emitting a DTPMOD64/DTPREL64 pair for x, we emit a single
R_PPC64_TPREL64.
Differential Revision: https://reviews.llvm.org/D48090
llvm-svn: 335651
2018-06-26 19:38:18 +00:00
|
|
|
// If this is a call to __tls_get_addr, it may be part of a TLS
|
|
|
|
// sequence that has been relaxed and turned into a nop. In this
|
|
|
|
// case, we don't want to handle it as a call.
|
|
|
|
if (read32(bufLoc) == 0x60000000) // nop
|
|
|
|
break;
|
|
|
|
|
2016-05-24 12:17:11 +00:00
|
|
|
// Patch a nop (0x60000000) to a ld.
|
2018-05-06 19:13:29 +00:00
|
|
|
if (rel.sym->needsTocRestore) {
|
|
|
|
if (bufLoc + 8 > bufEnd || read32(bufLoc + 4) != 0x60000000) {
|
|
|
|
error(getErrorLocation(bufLoc) + "call lacks nop, can't restore toc");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
write32(bufLoc + 4, 0xe8410018); // ld %r2, 24(%r1)
|
2018-04-23 15:01:24 +00:00
|
|
|
}
|
2018-04-23 20:34:35 +00:00
|
|
|
target->relocateOne(bufLoc, type, targetVA);
|
|
|
|
break;
|
2016-05-20 21:14:06 +00:00
|
|
|
default:
|
2016-12-14 22:45:52 +00:00
|
|
|
target->relocateOne(bufLoc, type, targetVA);
|
2016-05-20 21:14:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-09-21 22:01:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 23:16:02 +00:00
|
|
|
// For each function-defining prologue, find any calls to __morestack,
|
|
|
|
// and replace them with calls to __morestack_non_split.
|
|
|
|
static void switchMorestackCallsToMorestackNonSplit(
|
2018-08-01 08:10:50 +00:00
|
|
|
DenseSet<Defined *> &prologues, std::vector<Relocation *> &morestackCalls) {
|
2018-07-17 23:16:02 +00:00
|
|
|
|
|
|
|
// If the target adjusted a function's prologue, all calls to
|
|
|
|
// __morestack inside that function should be switched to
|
|
|
|
// __morestack_non_split.
|
|
|
|
Symbol *moreStackNonSplit = symtab->find("__morestack_non_split");
|
2018-08-02 18:13:40 +00:00
|
|
|
if (!moreStackNonSplit) {
|
|
|
|
error("Mixing split-stack objects requires a definition of "
|
|
|
|
"__morestack_non_split");
|
|
|
|
return;
|
|
|
|
}
|
2018-07-17 23:16:02 +00:00
|
|
|
|
|
|
|
// Sort both collections to compare addresses efficiently.
|
2018-09-26 20:54:42 +00:00
|
|
|
llvm::sort(morestackCalls, [](const Relocation *l, const Relocation *r) {
|
|
|
|
return l->offset < r->offset;
|
|
|
|
});
|
2018-07-17 23:16:02 +00:00
|
|
|
std::vector<Defined *> functions(prologues.begin(), prologues.end());
|
2018-09-26 20:54:42 +00:00
|
|
|
llvm::sort(functions, [](const Defined *l, const Defined *r) {
|
|
|
|
return l->value < r->value;
|
|
|
|
});
|
2018-07-17 23:16:02 +00:00
|
|
|
|
|
|
|
auto it = morestackCalls.begin();
|
|
|
|
for (Defined *f : functions) {
|
|
|
|
// Find the first call to __morestack within the function.
|
|
|
|
while (it != morestackCalls.end() && (*it)->offset < f->value)
|
|
|
|
++it;
|
|
|
|
// Adjust all calls inside the function.
|
|
|
|
while (it != morestackCalls.end() && (*it)->offset < f->value + f->size) {
|
|
|
|
(*it)->sym = moreStackNonSplit;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 18:13:40 +00:00
|
|
|
static bool enclosingPrologueAttempted(uint64_t offset,
|
|
|
|
const DenseSet<Defined *> &prologues) {
|
2018-07-17 23:16:02 +00:00
|
|
|
for (Defined *f : prologues)
|
|
|
|
if (f->value <= offset && offset < f->value + f->size)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a function compiled for split stack calls a function not
|
|
|
|
// compiled for split stack, then the caller needs its prologue
|
|
|
|
// adjusted to ensure that the called function will have enough stack
|
|
|
|
// available. Find those functions, and adjust their prologues.
|
|
|
|
template <class ELFT>
|
|
|
|
void InputSectionBase::adjustSplitStackFunctionPrologues(uint8_t *buf,
|
|
|
|
uint8_t *end) {
|
|
|
|
if (!getFile<ELFT>()->splitStack)
|
|
|
|
return;
|
2018-08-02 18:13:40 +00:00
|
|
|
DenseSet<Defined *> prologues;
|
2018-07-17 23:16:02 +00:00
|
|
|
std::vector<Relocation *> morestackCalls;
|
|
|
|
|
|
|
|
for (Relocation &rel : relocations) {
|
|
|
|
// Local symbols can't possibly be cross-calls, and should have been
|
|
|
|
// resolved long before this line.
|
|
|
|
if (rel.sym->isLocal())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Ignore calls into the split-stack api.
|
2018-08-13 22:29:15 +00:00
|
|
|
if (rel.sym->getName().startswith("__morestack")) {
|
|
|
|
if (rel.sym->getName().equals("__morestack"))
|
2018-07-17 23:16:02 +00:00
|
|
|
morestackCalls.push_back(&rel);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A relocation to non-function isn't relevant. Sometimes
|
|
|
|
// __morestack is not marked as a function, so this check comes
|
|
|
|
// after the name check.
|
2018-08-13 22:29:15 +00:00
|
|
|
if (rel.sym->type != STT_FUNC)
|
2018-07-17 23:16:02 +00:00
|
|
|
continue;
|
|
|
|
|
2018-08-13 22:29:15 +00:00
|
|
|
// If the callee's-file was compiled with split stack, nothing to do. In
|
|
|
|
// this context, a "Defined" symbol is one "defined by the binary currently
|
|
|
|
// being produced". So an "undefined" symbol might be provided by a shared
|
|
|
|
// library. It is not possible to tell how such symbols were compiled, so be
|
|
|
|
// conservative.
|
|
|
|
if (Defined *d = dyn_cast<Defined>(rel.sym))
|
|
|
|
if (InputSection *isec = cast_or_null<InputSection>(d->section))
|
2018-09-04 21:06:59 +00:00
|
|
|
if (!isec || !isec->getFile<ELFT>() || isec->getFile<ELFT>()->splitStack)
|
2018-08-13 22:29:15 +00:00
|
|
|
continue;
|
2018-08-02 18:13:40 +00:00
|
|
|
|
|
|
|
if (enclosingPrologueAttempted(rel.offset, prologues))
|
2018-07-17 23:16:02 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Defined *f = getEnclosingFunction<ELFT>(rel.offset)) {
|
2018-08-02 18:13:40 +00:00
|
|
|
prologues.insert(f);
|
|
|
|
if (target->adjustPrologueForCrossSplitStack(buf + getOffset(f->value),
|
2018-10-16 17:13:01 +00:00
|
|
|
end, f->stOther))
|
2018-07-17 23:16:02 +00:00
|
|
|
continue;
|
2018-08-02 18:13:40 +00:00
|
|
|
if (!getFile<ELFT>()->someNoSplitStack)
|
2019-10-07 08:31:18 +00:00
|
|
|
error(toString(this) + ": " + f->getName() +
|
2018-08-13 22:29:15 +00:00
|
|
|
" (with -fsplit-stack) calls " + rel.sym->getName() +
|
2018-08-02 18:13:40 +00:00
|
|
|
" (without -fsplit-stack), but couldn't adjust its prologue");
|
2018-07-17 23:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 17:13:01 +00:00
|
|
|
|
|
|
|
if (target->needsMoreStackNonSplit)
|
|
|
|
switchMorestackCallsToMorestackNonSplit(prologues, morestackCalls);
|
2018-07-17 23:16:02 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 16:49:07 +00:00
|
|
|
template <class ELFT> void InputSection::writeTo(uint8_t *buf) {
|
2017-12-19 16:29:02 +00:00
|
|
|
if (type == SHT_NOBITS)
|
2015-07-24 21:03:07 +00:00
|
|
|
return;
|
2016-02-25 08:23:37 +00:00
|
|
|
|
2017-02-27 02:56:02 +00:00
|
|
|
if (auto *s = dyn_cast<SyntheticSection>(this)) {
|
2016-11-23 09:47:38 +00:00
|
|
|
s->writeTo(buf + outSecOff);
|
2016-11-16 10:02:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-08 16:18:10 +00:00
|
|
|
// If -r or --emit-relocs is given, then an InputSection
|
|
|
|
// may be a relocation section.
|
2017-12-19 16:29:02 +00:00
|
|
|
if (type == SHT_RELA) {
|
|
|
|
copyRelocations<ELFT>(buf + outSecOff, getDataAs<typename ELFT::Rela>());
|
2016-02-25 08:23:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-12-19 16:29:02 +00:00
|
|
|
if (type == SHT_REL) {
|
|
|
|
copyRelocations<ELFT>(buf + outSecOff, getDataAs<typename ELFT::Rel>());
|
2016-02-25 08:23:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-09 03:19:08 +00:00
|
|
|
// If -r is given, we may have a SHT_GROUP section.
|
2017-12-19 16:29:02 +00:00
|
|
|
if (type == SHT_GROUP) {
|
2017-06-09 03:19:08 +00:00
|
|
|
copyShtGroup<ELFT>(buf + outSecOff);
|
2017-05-29 08:37:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
// If this is a compressed section, uncompress section contents directly
|
|
|
|
// to the buffer.
|
2019-03-12 20:32:30 +00:00
|
|
|
if (uncompressedSize >= 0) {
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
size_t size = uncompressedSize;
|
|
|
|
if (Error e = zlib::uncompress(toStringRef(rawData),
|
|
|
|
(char *)(buf + outSecOff), size))
|
|
|
|
fatal(toString(this) +
|
|
|
|
": uncompress failed: " + llvm::toString(std::move(e)));
|
|
|
|
uint8_t *bufEnd = buf + outSecOff + size;
|
|
|
|
relocate<ELFT>(buf, bufEnd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-14 01:35:04 +00:00
|
|
|
// Copy section contents from source object file to output file
|
|
|
|
// and then apply relocations.
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
memcpy(buf + outSecOff, data().data(), data().size());
|
|
|
|
uint8_t *bufEnd = buf + outSecOff + data().size();
|
2017-12-19 16:29:02 +00:00
|
|
|
relocate<ELFT>(buf, bufEnd);
|
2015-07-24 21:03:07 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 16:49:07 +00:00
|
|
|
void InputSection::replace(InputSection *other) {
|
2017-12-19 16:29:02 +00:00
|
|
|
alignment = std::max(alignment, other->alignment);
|
2019-05-29 03:55:20 +00:00
|
|
|
|
|
|
|
// When a section is replaced with another section that was allocated to
|
|
|
|
// another partition, the replacement section (and its associated sections)
|
|
|
|
// need to be placed in the main partition so that both partitions will be
|
|
|
|
// able to access it.
|
|
|
|
if (partition != other->partition) {
|
|
|
|
partition = 1;
|
|
|
|
for (InputSection *isec : dependentSections)
|
|
|
|
isec->partition = 1;
|
|
|
|
}
|
|
|
|
|
2017-12-19 16:29:02 +00:00
|
|
|
other->repl = repl;
|
2019-05-29 03:55:20 +00:00
|
|
|
other->markDead();
|
2016-02-25 18:43:51 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 19:54:14 +00:00
|
|
|
template <class ELFT>
|
2017-12-21 02:03:39 +00:00
|
|
|
EhInputSection::EhInputSection(ObjFile<ELFT> &f,
|
|
|
|
const typename ELFT::Shdr &header,
|
2017-03-06 21:17:18 +00:00
|
|
|
StringRef name)
|
2017-11-30 14:01:06 +00:00
|
|
|
: InputSectionBase(f, header, name, InputSectionBase::EHFrame) {}
|
2015-11-11 19:54:14 +00:00
|
|
|
|
2017-05-31 20:17:44 +00:00
|
|
|
SyntheticSection *EhInputSection::getParent() const {
|
|
|
|
return cast_or_null<SyntheticSection>(parent);
|
|
|
|
}
|
|
|
|
|
2016-07-21 20:18:30 +00:00
|
|
|
// Returns the index of the first relocation that points to a region between
|
|
|
|
// Begin and Begin+Size.
|
|
|
|
template <class IntTy, class RelTy>
|
|
|
|
static unsigned getReloc(IntTy begin, IntTy size, const ArrayRef<RelTy> &rels,
|
|
|
|
unsigned &relocI) {
|
|
|
|
// Start search from RelocI for fast access. That works because the
|
|
|
|
// relocations are sorted in .eh_frame.
|
|
|
|
for (unsigned n = rels.size(); relocI < n; ++relocI) {
|
|
|
|
const RelTy &rel = rels[relocI];
|
|
|
|
if (rel.r_offset < begin)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (rel.r_offset < begin + size)
|
|
|
|
return relocI;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-22 23:53:00 +00:00
|
|
|
// .eh_frame is a sequence of CIE or FDE records.
|
|
|
|
// This function splits an input section into records and returns them.
|
2017-03-06 21:17:18 +00:00
|
|
|
template <class ELFT> void EhInputSection::split() {
|
2017-12-19 16:29:02 +00:00
|
|
|
if (areRelocsRela)
|
|
|
|
split<ELFT>(relas<ELFT>());
|
2017-10-25 18:09:54 +00:00
|
|
|
else
|
2017-12-19 16:29:02 +00:00
|
|
|
split<ELFT>(rels<ELFT>());
|
2016-07-21 20:18:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 21:17:18 +00:00
|
|
|
template <class ELFT, class RelTy>
|
|
|
|
void EhInputSection::split(ArrayRef<RelTy> rels) {
|
2016-07-21 20:18:30 +00:00
|
|
|
unsigned relI = 0;
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
for (size_t off = 0, end = data().size(); off != end;) {
|
2017-10-27 03:14:09 +00:00
|
|
|
size_t size = readEhRecordSize(this, off);
|
2017-12-19 16:29:02 +00:00
|
|
|
pieces.emplace_back(off, this, size, getReloc(off, size, rels, relI));
|
2016-05-22 23:53:00 +00:00
|
|
|
// The empty record is the end marker.
|
|
|
|
if (size == 4)
|
|
|
|
break;
|
|
|
|
off += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 01:58:30 +00:00
|
|
|
static size_t findNull(StringRef s, size_t entSize) {
|
2016-04-22 22:09:35 +00:00
|
|
|
// Optimize the common case.
|
|
|
|
if (entSize == 1)
|
|
|
|
return s.find(0);
|
|
|
|
|
|
|
|
for (unsigned i = 0, n = s.size(); i != n; i += entSize) {
|
|
|
|
const char *b = s.begin() + i;
|
|
|
|
if (std::all_of(b, b + entSize, [](char c) { return c == 0; }))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return StringRef::npos;
|
|
|
|
}
|
|
|
|
|
2017-05-31 20:17:44 +00:00
|
|
|
SyntheticSection *MergeInputSection::getParent() const {
|
|
|
|
return cast_or_null<SyntheticSection>(parent);
|
|
|
|
}
|
|
|
|
|
2016-05-23 00:40:24 +00:00
|
|
|
// Split SHF_STRINGS section. Such section is a sequence of
|
|
|
|
// null-terminated strings.
|
2017-03-06 20:23:56 +00:00
|
|
|
void MergeInputSection::splitStrings(ArrayRef<uint8_t> data, size_t entSize) {
|
2016-05-23 00:40:24 +00:00
|
|
|
size_t off = 0;
|
2017-12-19 16:29:02 +00:00
|
|
|
bool isAlloc = flags & SHF_ALLOC;
|
2017-10-22 01:58:30 +00:00
|
|
|
StringRef s = toStringRef(data);
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 05:00:37 +00:00
|
|
|
|
2017-10-22 01:58:30 +00:00
|
|
|
while (!s.empty()) {
|
|
|
|
size_t end = findNull(s, entSize);
|
2016-05-23 00:40:24 +00:00
|
|
|
if (end == StringRef::npos)
|
2016-11-23 18:07:33 +00:00
|
|
|
fatal(toString(this) + ": string is not null terminated");
|
2016-05-23 00:40:24 +00:00
|
|
|
size_t size = end + entSize;
|
2017-10-21 23:20:13 +00:00
|
|
|
|
2017-10-22 01:58:30 +00:00
|
|
|
pieces.emplace_back(off, xxHash64(s.substr(0, size)), !isAlloc);
|
|
|
|
s = s.substr(size);
|
2016-05-23 00:40:24 +00:00
|
|
|
off += size;
|
2016-04-22 22:09:35 +00:00
|
|
|
}
|
2016-05-23 00:40:24 +00:00
|
|
|
}
|
2016-04-22 22:09:35 +00:00
|
|
|
|
2016-05-23 00:40:24 +00:00
|
|
|
// Split non-SHF_STRINGS section. Such section is a sequence of
|
|
|
|
// fixed size records.
|
2017-03-06 20:23:56 +00:00
|
|
|
void MergeInputSection::splitNonStrings(ArrayRef<uint8_t> data,
|
|
|
|
size_t entSize) {
|
2016-04-22 22:09:35 +00:00
|
|
|
size_t size = data.size();
|
|
|
|
assert((size % entSize) == 0);
|
2017-12-19 16:29:02 +00:00
|
|
|
bool isAlloc = flags & SHF_ALLOC;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 05:00:37 +00:00
|
|
|
|
2017-10-21 23:20:13 +00:00
|
|
|
for (size_t i = 0; i != size; i += entSize)
|
2018-07-31 18:13:36 +00:00
|
|
|
pieces.emplace_back(i, xxHash64(data.slice(i, entSize)), !isAlloc);
|
2016-05-23 00:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2017-12-21 02:03:39 +00:00
|
|
|
MergeInputSection::MergeInputSection(ObjFile<ELFT> &f,
|
|
|
|
const typename ELFT::Shdr &header,
|
2017-03-06 20:23:56 +00:00
|
|
|
StringRef name)
|
2017-12-21 01:21:59 +00:00
|
|
|
: InputSectionBase(f, header, name, InputSectionBase::Merge) {}
|
|
|
|
|
|
|
|
MergeInputSection::MergeInputSection(uint64_t flags, uint32_t type,
|
|
|
|
uint64_t entsize, ArrayRef<uint8_t> data,
|
|
|
|
StringRef name)
|
|
|
|
: InputSectionBase(nullptr, flags, type, entsize, /*Link*/ 0, /*Info*/ 0,
|
|
|
|
/*Alignment*/ entsize, data, name, SectionBase::Merge) {}
|
2016-05-23 16:55:43 +00:00
|
|
|
|
Parallelize uncompress() and splitIntoPieces().
Uncompressing section contents and spliting mergeable section contents
into smaller chunks are heavy tasks. They scan entire section contents
and do CPU-intensive tasks such as uncompressing zlib-compressed data
or computing a hash value for each section piece.
Luckily, these tasks are independent to each other, so we can do that
in parallel_for_each. The number of input sections is large (as opposed
to the number of output sections), so there's a large parallelism here.
Actually the current design to call uncompress() and splitIntoPieces()
in batch was chosen with doing this in mind. Basically what we need to
do here is to replace `for` with `parallel_for_each`.
It seems this patch improves latency significantly if linked programs
contain debug info (which in turn contain lots of mergeable strings.)
For example, the latency to link Clang (debug build) improved by 20% on
my machine as shown below. Note that ld.gold took 19.2 seconds to do
the same thing.
Before:
30801.782712 task-clock (msec) # 3.652 CPUs utilized ( +- 2.59% )
104,084 context-switches # 0.003 M/sec ( +- 1.02% )
5,063 cpu-migrations # 0.164 K/sec ( +- 13.66% )
2,528,130 page-faults # 0.082 M/sec ( +- 0.47% )
85,317,809,130 cycles # 2.770 GHz ( +- 2.62% )
67,352,463,373 stalled-cycles-frontend # 78.94% frontend cycles idle ( +- 3.06% )
<not supported> stalled-cycles-backend
44,295,945,493 instructions # 0.52 insns per cycle
# 1.52 stalled cycles per insn ( +- 0.44% )
8,572,384,877 branches # 278.308 M/sec ( +- 0.66% )
141,806,726 branch-misses # 1.65% of all branches ( +- 0.13% )
8.433424003 seconds time elapsed ( +- 1.20% )
After:
35523.764575 task-clock (msec) # 5.265 CPUs utilized ( +- 2.67% )
159,107 context-switches # 0.004 M/sec ( +- 0.48% )
8,123 cpu-migrations # 0.229 K/sec ( +- 23.34% )
2,372,483 page-faults # 0.067 M/sec ( +- 0.36% )
98,395,342,152 cycles # 2.770 GHz ( +- 2.62% )
79,294,670,125 stalled-cycles-frontend # 80.59% frontend cycles idle ( +- 3.03% )
<not supported> stalled-cycles-backend
46,274,151,813 instructions # 0.47 insns per cycle
# 1.71 stalled cycles per insn ( +- 0.47% )
8,987,621,670 branches # 253.003 M/sec ( +- 0.60% )
148,900,624 branch-misses # 1.66% of all branches ( +- 0.27% )
6.747548004 seconds time elapsed ( +- 0.40% )
llvm-svn: 287946
2016-11-25 20:05:08 +00:00
|
|
|
// This function is called after we obtain a complete list of input sections
|
|
|
|
// that need to be linked. This is responsible to split section contents
|
|
|
|
// into small chunks for further processing.
|
|
|
|
//
|
2017-08-23 19:03:20 +00:00
|
|
|
// Note that this function is called from parallelForEach. This must be
|
Parallelize uncompress() and splitIntoPieces().
Uncompressing section contents and spliting mergeable section contents
into smaller chunks are heavy tasks. They scan entire section contents
and do CPU-intensive tasks such as uncompressing zlib-compressed data
or computing a hash value for each section piece.
Luckily, these tasks are independent to each other, so we can do that
in parallel_for_each. The number of input sections is large (as opposed
to the number of output sections), so there's a large parallelism here.
Actually the current design to call uncompress() and splitIntoPieces()
in batch was chosen with doing this in mind. Basically what we need to
do here is to replace `for` with `parallel_for_each`.
It seems this patch improves latency significantly if linked programs
contain debug info (which in turn contain lots of mergeable strings.)
For example, the latency to link Clang (debug build) improved by 20% on
my machine as shown below. Note that ld.gold took 19.2 seconds to do
the same thing.
Before:
30801.782712 task-clock (msec) # 3.652 CPUs utilized ( +- 2.59% )
104,084 context-switches # 0.003 M/sec ( +- 1.02% )
5,063 cpu-migrations # 0.164 K/sec ( +- 13.66% )
2,528,130 page-faults # 0.082 M/sec ( +- 0.47% )
85,317,809,130 cycles # 2.770 GHz ( +- 2.62% )
67,352,463,373 stalled-cycles-frontend # 78.94% frontend cycles idle ( +- 3.06% )
<not supported> stalled-cycles-backend
44,295,945,493 instructions # 0.52 insns per cycle
# 1.52 stalled cycles per insn ( +- 0.44% )
8,572,384,877 branches # 278.308 M/sec ( +- 0.66% )
141,806,726 branch-misses # 1.65% of all branches ( +- 0.13% )
8.433424003 seconds time elapsed ( +- 1.20% )
After:
35523.764575 task-clock (msec) # 5.265 CPUs utilized ( +- 2.67% )
159,107 context-switches # 0.004 M/sec ( +- 0.48% )
8,123 cpu-migrations # 0.229 K/sec ( +- 23.34% )
2,372,483 page-faults # 0.067 M/sec ( +- 0.36% )
98,395,342,152 cycles # 2.770 GHz ( +- 2.62% )
79,294,670,125 stalled-cycles-frontend # 80.59% frontend cycles idle ( +- 3.03% )
<not supported> stalled-cycles-backend
46,274,151,813 instructions # 0.47 insns per cycle
# 1.71 stalled cycles per insn ( +- 0.47% )
8,987,621,670 branches # 253.003 M/sec ( +- 0.60% )
148,900,624 branch-misses # 1.66% of all branches ( +- 0.27% )
6.747548004 seconds time elapsed ( +- 0.40% )
llvm-svn: 287946
2016-11-25 20:05:08 +00:00
|
|
|
// thread-safe (i.e. no memory allocation from the pools).
|
2017-03-06 20:23:56 +00:00
|
|
|
void MergeInputSection::splitIntoPieces() {
|
2017-09-01 12:04:52 +00:00
|
|
|
assert(pieces.empty());
|
2017-10-22 01:58:30 +00:00
|
|
|
|
2017-12-19 16:29:02 +00:00
|
|
|
if (flags & SHF_STRINGS)
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
splitStrings(data(), entsize);
|
2016-05-23 00:40:24 +00:00
|
|
|
else
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
splitNonStrings(data(), entsize);
|
2016-04-22 22:09:35 +00:00
|
|
|
}
|
2015-10-19 21:00:02 +00:00
|
|
|
|
2018-04-03 04:06:14 +00:00
|
|
|
SectionPiece *MergeInputSection::getSectionPiece(uint64_t offset) {
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-08 16:58:59 +00:00
|
|
|
if (this->data().size() <= offset)
|
2018-08-31 11:51:51 +00:00
|
|
|
fatal(toString(this) + ": offset is outside the section");
|
|
|
|
|
2018-04-03 04:06:14 +00:00
|
|
|
// If Offset is not at beginning of a section piece, it is not in the map.
|
2018-08-31 11:51:51 +00:00
|
|
|
// In that case we need to do a binary search of the original section piece vector.
|
2019-06-30 11:19:56 +00:00
|
|
|
auto it = partition_point(
|
|
|
|
pieces, [=](SectionPiece p) { return p.inputOff <= offset; });
|
2019-04-17 08:00:46 +00:00
|
|
|
return &it[-1];
|
2018-04-03 04:06:14 +00:00
|
|
|
}
|
|
|
|
|
Avoid doing binary search.
MergedInputSection::getOffset is the busiest function in LLD if string
merging is enabled and input files have lots of mergeable sections.
It is usually the case when creating executable with debug info,
so it is pretty common.
The reason why it is slow is because it has to do faily complex
computations. For non-mergeable sections, section contents are
contiguous in output, so in order to compute an output offset,
we only have to add the output section's base address to an input
offset. But for mergeable strings, section contents are split for
merging, so they are not contigous. We've got to do some lookups.
We used to do binary search on the list of section pieces.
It is slow because I think it's hostile to branch prediction.
This patch replaces it with hash table lookup. Seems it's working
pretty well. Below is "perf stat -r10" output when linking clang
with debug info. In this case this patch speeds up about 4%.
Before:
6584.153205 task-clock (msec) # 1.001 CPUs utilized ( +- 0.09% )
238 context-switches # 0.036 K/sec ( +- 6.59% )
0 cpu-migrations # 0.000 K/sec ( +- 50.92% )
1,067,675 page-faults # 0.162 M/sec ( +- 0.15% )
18,369,931,470 cycles # 2.790 GHz ( +- 0.09% )
9,640,680,143 stalled-cycles-frontend # 52.48% frontend cycles idle ( +- 0.18% )
<not supported> stalled-cycles-backend
21,206,747,787 instructions # 1.15 insns per cycle
# 0.45 stalled cycles per insn ( +- 0.04% )
3,817,398,032 branches # 579.786 M/sec ( +- 0.04% )
132,787,249 branch-misses # 3.48% of all branches ( +- 0.02% )
6.579106511 seconds time elapsed ( +- 0.09% )
After:
6312.317533 task-clock (msec) # 1.001 CPUs utilized ( +- 0.19% )
221 context-switches # 0.035 K/sec ( +- 4.11% )
1 cpu-migrations # 0.000 K/sec ( +- 45.21% )
1,280,775 page-faults # 0.203 M/sec ( +- 0.37% )
17,611,539,150 cycles # 2.790 GHz ( +- 0.19% )
10,285,148,569 stalled-cycles-frontend # 58.40% frontend cycles idle ( +- 0.30% )
<not supported> stalled-cycles-backend
18,794,779,900 instructions # 1.07 insns per cycle
# 0.55 stalled cycles per insn ( +- 0.03% )
3,287,450,865 branches # 520.799 M/sec ( +- 0.03% )
72,259,605 branch-misses # 2.20% of all branches ( +- 0.01% )
6.307411828 seconds time elapsed ( +- 0.19% )
Differential Revision: http://reviews.llvm.org/D20645
llvm-svn: 270999
2016-05-27 14:39:13 +00:00
|
|
|
// Returns the offset in an output section for a given input offset.
|
|
|
|
// Because contents of a mergeable section is not contiguous in output,
|
|
|
|
// it is not just an addition to a base output offset.
|
2018-04-19 16:05:07 +00:00
|
|
|
uint64_t MergeInputSection::getParentOffset(uint64_t offset) const {
|
2017-10-31 19:14:06 +00:00
|
|
|
// If Offset is not at beginning of a section piece, it is not in the map.
|
|
|
|
// In that case we need to search from the original section piece vector.
|
2018-04-03 04:06:14 +00:00
|
|
|
const SectionPiece &piece =
|
2018-08-31 11:51:51 +00:00
|
|
|
*(const_cast<MergeInputSection *>(this)->getSectionPiece (offset));
|
2017-03-06 20:23:56 +00:00
|
|
|
uint64_t addend = offset - piece.inputOff;
|
2016-05-28 18:40:38 +00:00
|
|
|
return piece.outputOff + addend;
|
Avoid doing binary search.
MergedInputSection::getOffset is the busiest function in LLD if string
merging is enabled and input files have lots of mergeable sections.
It is usually the case when creating executable with debug info,
so it is pretty common.
The reason why it is slow is because it has to do faily complex
computations. For non-mergeable sections, section contents are
contiguous in output, so in order to compute an output offset,
we only have to add the output section's base address to an input
offset. But for mergeable strings, section contents are split for
merging, so they are not contigous. We've got to do some lookups.
We used to do binary search on the list of section pieces.
It is slow because I think it's hostile to branch prediction.
This patch replaces it with hash table lookup. Seems it's working
pretty well. Below is "perf stat -r10" output when linking clang
with debug info. In this case this patch speeds up about 4%.
Before:
6584.153205 task-clock (msec) # 1.001 CPUs utilized ( +- 0.09% )
238 context-switches # 0.036 K/sec ( +- 6.59% )
0 cpu-migrations # 0.000 K/sec ( +- 50.92% )
1,067,675 page-faults # 0.162 M/sec ( +- 0.15% )
18,369,931,470 cycles # 2.790 GHz ( +- 0.09% )
9,640,680,143 stalled-cycles-frontend # 52.48% frontend cycles idle ( +- 0.18% )
<not supported> stalled-cycles-backend
21,206,747,787 instructions # 1.15 insns per cycle
# 0.45 stalled cycles per insn ( +- 0.04% )
3,817,398,032 branches # 579.786 M/sec ( +- 0.04% )
132,787,249 branch-misses # 3.48% of all branches ( +- 0.02% )
6.579106511 seconds time elapsed ( +- 0.09% )
After:
6312.317533 task-clock (msec) # 1.001 CPUs utilized ( +- 0.19% )
221 context-switches # 0.035 K/sec ( +- 4.11% )
1 cpu-migrations # 0.000 K/sec ( +- 45.21% )
1,280,775 page-faults # 0.203 M/sec ( +- 0.37% )
17,611,539,150 cycles # 2.790 GHz ( +- 0.19% )
10,285,148,569 stalled-cycles-frontend # 58.40% frontend cycles idle ( +- 0.30% )
<not supported> stalled-cycles-backend
18,794,779,900 instructions # 1.07 insns per cycle
# 0.55 stalled cycles per insn ( +- 0.03% )
3,287,450,865 branches # 520.799 M/sec ( +- 0.03% )
72,259,605 branch-misses # 2.20% of all branches ( +- 0.01% )
6.307411828 seconds time elapsed ( +- 0.19% )
Differential Revision: http://reviews.llvm.org/D20645
llvm-svn: 270999
2016-05-27 14:39:13 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 02:03:39 +00:00
|
|
|
template InputSection::InputSection(ObjFile<ELF32LE> &, const ELF32LE::Shdr &,
|
2017-07-26 22:13:32 +00:00
|
|
|
StringRef);
|
2017-12-21 02:03:39 +00:00
|
|
|
template InputSection::InputSection(ObjFile<ELF32BE> &, const ELF32BE::Shdr &,
|
2017-07-26 22:13:32 +00:00
|
|
|
StringRef);
|
2017-12-21 02:03:39 +00:00
|
|
|
template InputSection::InputSection(ObjFile<ELF64LE> &, const ELF64LE::Shdr &,
|
2017-07-26 22:13:32 +00:00
|
|
|
StringRef);
|
2017-12-21 02:03:39 +00:00
|
|
|
template InputSection::InputSection(ObjFile<ELF64BE> &, const ELF64BE::Shdr &,
|
2017-07-26 22:13:32 +00:00
|
|
|
StringRef);
|
2017-03-29 20:15:29 +00:00
|
|
|
|
|
|
|
template std::string InputSectionBase::getLocation<ELF32LE>(uint64_t);
|
|
|
|
template std::string InputSectionBase::getLocation<ELF32BE>(uint64_t);
|
|
|
|
template std::string InputSectionBase::getLocation<ELF64LE>(uint64_t);
|
|
|
|
template std::string InputSectionBase::getLocation<ELF64BE>(uint64_t);
|
|
|
|
|
|
|
|
template void InputSection::writeTo<ELF32LE>(uint8_t *);
|
|
|
|
template void InputSection::writeTo<ELF32BE>(uint8_t *);
|
|
|
|
template void InputSection::writeTo<ELF64LE>(uint8_t *);
|
|
|
|
template void InputSection::writeTo<ELF64BE>(uint8_t *);
|
2016-02-28 00:25:54 +00:00
|
|
|
|
2017-12-21 02:03:39 +00:00
|
|
|
template MergeInputSection::MergeInputSection(ObjFile<ELF32LE> &,
|
|
|
|
const ELF32LE::Shdr &, StringRef);
|
|
|
|
template MergeInputSection::MergeInputSection(ObjFile<ELF32BE> &,
|
|
|
|
const ELF32BE::Shdr &, StringRef);
|
|
|
|
template MergeInputSection::MergeInputSection(ObjFile<ELF64LE> &,
|
|
|
|
const ELF64LE::Shdr &, StringRef);
|
|
|
|
template MergeInputSection::MergeInputSection(ObjFile<ELF64BE> &,
|
|
|
|
const ELF64BE::Shdr &, StringRef);
|
|
|
|
|
|
|
|
template EhInputSection::EhInputSection(ObjFile<ELF32LE> &,
|
|
|
|
const ELF32LE::Shdr &, StringRef);
|
|
|
|
template EhInputSection::EhInputSection(ObjFile<ELF32BE> &,
|
|
|
|
const ELF32BE::Shdr &, StringRef);
|
|
|
|
template EhInputSection::EhInputSection(ObjFile<ELF64LE> &,
|
|
|
|
const ELF64LE::Shdr &, StringRef);
|
|
|
|
template EhInputSection::EhInputSection(ObjFile<ELF64BE> &,
|
|
|
|
const ELF64BE::Shdr &, StringRef);
|
2017-03-06 21:17:18 +00:00
|
|
|
|
|
|
|
template void EhInputSection::split<ELF32LE>();
|
|
|
|
template void EhInputSection::split<ELF32BE>();
|
|
|
|
template void EhInputSection::split<ELF64LE>();
|
|
|
|
template void EhInputSection::split<ELF64BE>();
|
2019-10-07 08:31:18 +00:00
|
|
|
|
|
|
|
} // namespace elf
|
|
|
|
} // namespace lld
|