2015-09-22 05:38:08 +08:00
|
|
|
//===- OutputSections.h -----------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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-09-22 05:38:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_ELF_OUTPUT_SECTIONS_H
|
|
|
|
#define LLD_ELF_OUTPUT_SECTIONS_H
|
|
|
|
|
2016-03-14 04:28:29 +08:00
|
|
|
#include "Config.h"
|
2017-03-09 06:36:28 +08:00
|
|
|
#include "InputSection.h"
|
2017-07-28 03:22:43 +08:00
|
|
|
#include "LinkerScript.h"
|
2016-06-20 05:39:37 +08:00
|
|
|
#include "Relocations.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
2015-09-22 05:38:08 +08:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
|
|
|
#include "llvm/Object/ELF.h"
|
2018-11-15 05:05:20 +08:00
|
|
|
#include <array>
|
2015-09-22 05:38:08 +08:00
|
|
|
|
|
|
|
namespace lld {
|
2016-02-28 08:25:54 +08:00
|
|
|
namespace elf {
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2016-12-20 01:01:01 +08:00
|
|
|
struct PhdrEntry;
|
2017-02-24 00:49:07 +08:00
|
|
|
class InputSection;
|
2017-02-23 10:28:28 +08:00
|
|
|
class InputSectionBase;
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2015-09-22 08:16:19 +08:00
|
|
|
// This represents a section in an output file.
|
2017-02-24 23:07:30 +08:00
|
|
|
// It is composed of multiple InputSections.
|
2015-09-22 08:16:19 +08:00
|
|
|
// The writer creates multiple OutputSections and assign them unique,
|
2015-09-22 05:38:08 +08:00
|
|
|
// non-overlapping file offsets and VAs.
|
2017-07-28 03:22:43 +08:00
|
|
|
class OutputSection final : public BaseCommand, public SectionBase {
|
2015-09-22 05:38:08 +08:00
|
|
|
public:
|
2017-02-24 23:07:30 +08:00
|
|
|
OutputSection(StringRef name, uint32_t type, uint64_t flags);
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2017-03-09 06:36:28 +08:00
|
|
|
static bool classof(const SectionBase *s) {
|
|
|
|
return s->kind() == SectionBase::Output;
|
|
|
|
}
|
2017-10-11 10:28:28 +08:00
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
static bool classof(const BaseCommand *c);
|
2017-03-09 06:36:28 +08:00
|
|
|
|
2018-01-26 03:02:08 +08:00
|
|
|
uint64_t getLMA() const { return ptLoad ? addr + ptLoad->lmaOffset : addr; }
|
2016-11-10 07:23:45 +08:00
|
|
|
template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *sHdr);
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2018-03-08 03:25:36 +08:00
|
|
|
uint32_t sectionIndex = UINT32_MAX;
|
2017-05-12 22:52:22 +08:00
|
|
|
unsigned sortRank;
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2016-07-27 22:10:56 +08:00
|
|
|
uint32_t getPhdrFlags() const;
|
2016-07-14 13:46:24 +08:00
|
|
|
|
2017-09-07 18:53:07 +08:00
|
|
|
// Pointer to the PT_LOAD segment, which this section resides in. This field
|
|
|
|
// is used to correctly compute file offset of a section. When two sections
|
|
|
|
// share the same load segment, difference between their file offsets should
|
|
|
|
// be equal to difference between their virtual addresses. To compute some
|
|
|
|
// section offset we use the following formula: Off = Off_first + VA -
|
|
|
|
// VA_first, where Off_first and VA_first is file offset and VA of first
|
|
|
|
// section in PT_LOAD.
|
|
|
|
PhdrEntry *ptLoad = nullptr;
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2017-06-07 17:20:35 +08:00
|
|
|
// Pointer to a relocation section for this section. Usually nullptr because
|
|
|
|
// we consume relocations, but if --emit-relocs is specified (which is rare),
|
|
|
|
// it may have a non-null value.
|
|
|
|
OutputSection *relocationSection = nullptr;
|
|
|
|
|
[ELF] Reset OutputSection size prior to processing linker script commands
The size of an OutputSection is calculated early, to aid handling of compressed
debug sections. However, subsequent to this point, unused synthetic sections are
removed. In the event that an OutputSection, from which such an InputSection is
removed, is still required (e.g. because it has a symbol assignment), and no longer
has any InputSections, dot assignments, or BYTE()-family directives, the size
member is never updated when processing the commands. If the removed InputSection
had a non-zero size (such as a .got.plt section), the section ends up with the
wrong size in the output.
The fix is to reset the OutputSection size prior to processing the linker script
commands relating to that OutputSection. This ensures that the size is correct even
in the above situation.
Additionally, to reduce the risk of developers misusing OutputSection Size and
InputSection OutSecOff, they are set to simply the number of InputSections in an
OutputSection, and the corresponding index respectively. We cannot completely
stop using them, due to SHF_LINK_ORDER sections requiring them.
Compressed debug sections also require the full size. This is now calculated in
maybeCompress for these kinds of sections.
Reviewers: ruiu, rafael
Differential Revision: https://reviews.llvm.org/D38361
llvm-svn: 320472
2017-12-12 19:51:13 +08:00
|
|
|
// Initially this field is the number of InputSections that have been added to
|
|
|
|
// the OutputSection so far. Later on, after a call to assignAddresses, it
|
|
|
|
// corresponds to the Elf_Shdr member.
|
2016-11-10 07:23:45 +08:00
|
|
|
uint64_t size = 0;
|
[ELF] Reset OutputSection size prior to processing linker script commands
The size of an OutputSection is calculated early, to aid handling of compressed
debug sections. However, subsequent to this point, unused synthetic sections are
removed. In the event that an OutputSection, from which such an InputSection is
removed, is still required (e.g. because it has a symbol assignment), and no longer
has any InputSections, dot assignments, or BYTE()-family directives, the size
member is never updated when processing the commands. If the removed InputSection
had a non-zero size (such as a .got.plt section), the section ends up with the
wrong size in the output.
The fix is to reset the OutputSection size prior to processing the linker script
commands relating to that OutputSection. This ensures that the size is correct even
in the above situation.
Additionally, to reduce the risk of developers misusing OutputSection Size and
InputSection OutSecOff, they are set to simply the number of InputSections in an
OutputSection, and the corresponding index respectively. We cannot completely
stop using them, due to SHF_LINK_ORDER sections requiring them.
Compressed debug sections also require the full size. This is now calculated in
maybeCompress for these kinds of sections.
Reviewers: ruiu, rafael
Differential Revision: https://reviews.llvm.org/D38361
llvm-svn: 320472
2017-12-12 19:51:13 +08:00
|
|
|
|
|
|
|
// The following fields correspond to Elf_Shdr members.
|
2016-11-10 07:23:45 +08:00
|
|
|
uint64_t offset = 0;
|
|
|
|
uint64_t addr = 0;
|
2016-11-09 09:42:41 +08:00
|
|
|
uint32_t shName = 0;
|
2016-08-11 02:10:41 +08:00
|
|
|
|
2019-09-24 19:48:31 +08:00
|
|
|
void recordSection(InputSectionBase *isec);
|
|
|
|
void commitSection(InputSection *isec);
|
|
|
|
void finalizeInputSections();
|
2016-11-29 16:05:44 +08:00
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
// The following members are normally only used in linker scripts.
|
|
|
|
MemoryRegion *memRegion = nullptr;
|
2018-01-25 09:36:36 +08:00
|
|
|
MemoryRegion *lmaRegion = nullptr;
|
2017-07-28 03:22:43 +08:00
|
|
|
Expr addrExpr;
|
|
|
|
Expr alignExpr;
|
|
|
|
Expr lmaExpr;
|
|
|
|
Expr subalignExpr;
|
2017-10-11 09:50:56 +08:00
|
|
|
std::vector<BaseCommand *> sectionCommands;
|
2017-07-28 03:22:43 +08:00
|
|
|
std::vector<StringRef> phdrs;
|
2018-11-15 05:05:20 +08:00
|
|
|
llvm::Optional<std::array<uint8_t, 4>> filler;
|
2017-07-28 03:22:43 +08:00
|
|
|
ConstraintKind constraint = ConstraintKind::NoConstraint;
|
|
|
|
std::string location;
|
|
|
|
std::string memoryRegionName;
|
2018-01-12 17:07:35 +08:00
|
|
|
std::string lmaRegionName;
|
2018-02-16 18:42:58 +08:00
|
|
|
bool nonAlloc = false;
|
2017-07-28 03:22:43 +08:00
|
|
|
bool noload = false;
|
2018-03-01 20:27:04 +08:00
|
|
|
bool expressionsUseSymbols = false;
|
2019-04-26 14:59:30 +08:00
|
|
|
bool usedInExpression = false;
|
2018-06-27 16:08:12 +08:00
|
|
|
bool inOverlay = false;
|
2017-07-28 03:22:43 +08:00
|
|
|
|
2019-06-04 04:14:25 +08:00
|
|
|
// Tracks whether the section has ever had an input section added to it, even
|
|
|
|
// if the section was later removed (e.g. because it is a synthetic section
|
|
|
|
// that wasn't needed). This is needed for orphan placement.
|
|
|
|
bool hasInputSections = false;
|
|
|
|
|
2019-03-06 11:07:57 +08:00
|
|
|
void finalize();
|
2017-07-28 03:22:43 +08:00
|
|
|
template <class ELFT> void writeTo(uint8_t *buf);
|
2021-07-09 17:05:18 +08:00
|
|
|
// Check that the addends for dynamic relocations were written correctly.
|
|
|
|
void checkDynRelAddends(const uint8_t *bufStart);
|
2017-07-28 03:22:43 +08:00
|
|
|
template <class ELFT> void maybeCompress();
|
|
|
|
|
2018-06-16 20:11:34 +08:00
|
|
|
void sort(llvm::function_ref<int(InputSectionBase *s)> order);
|
2017-07-28 03:22:43 +08:00
|
|
|
void sortInitFini();
|
|
|
|
void sortCtorsDtors();
|
2017-10-07 05:42:37 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Used for implementation of --compress-debug-sections option.
|
2017-12-13 01:37:01 +08:00
|
|
|
std::vector<uint8_t> zDebugHeader;
|
2020-11-30 06:01:32 +08:00
|
|
|
llvm::SmallVector<char, 0> compressedData;
|
2017-12-13 01:37:01 +08:00
|
|
|
|
2018-11-15 05:05:20 +08:00
|
|
|
std::array<uint8_t, 4> getFiller();
|
2017-12-13 01:37:01 +08:00
|
|
|
};
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
int getPriority(StringRef s);
|
|
|
|
|
2020-01-16 21:23:08 +08:00
|
|
|
InputSection *getFirstInputSection(const OutputSection *os);
|
|
|
|
std::vector<InputSection *> getInputSections(const OutputSection *os);
|
2018-02-22 17:55:28 +08:00
|
|
|
|
2017-02-27 10:31:26 +08:00
|
|
|
// All output sections that are handled by the linker specially are
|
2015-10-08 03:18:16 +08:00
|
|
|
// globally accessible. Writer initializes them, so don't use them
|
|
|
|
// until Writer is initialized.
|
2017-02-27 10:31:26 +08:00
|
|
|
struct Out {
|
2019-03-01 07:11:35 +08:00
|
|
|
static uint8_t *bufferStart;
|
2016-11-02 07:12:51 +08:00
|
|
|
static uint8_t first;
|
2016-12-20 01:01:01 +08:00
|
|
|
static PhdrEntry *tlsPhdr;
|
2017-02-24 23:07:30 +08:00
|
|
|
static OutputSection *elfHeader;
|
|
|
|
static OutputSection *programHeaders;
|
|
|
|
static OutputSection *preinitArray;
|
|
|
|
static OutputSection *initArray;
|
|
|
|
static OutputSection *finiArray;
|
2015-10-08 03:18:16 +08:00
|
|
|
};
|
2015-10-10 03:34:55 +08:00
|
|
|
|
2017-03-13 22:40:58 +08:00
|
|
|
uint64_t getHeaderSize();
|
2016-09-23 00:47:21 +08:00
|
|
|
|
2017-06-14 07:26:31 +08:00
|
|
|
extern std::vector<OutputSection *> outputSections;
|
2016-02-28 08:25:54 +08:00
|
|
|
} // namespace elf
|
2015-11-04 10:11:57 +08:00
|
|
|
} // namespace lld
|
|
|
|
|
2016-07-12 17:49:43 +08:00
|
|
|
#endif
|