blob: 1fe793be0c88b06eba7b8800c9feecf7073be846 [file] [log] [blame]
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001//===- OutputSections.h -----------------------------------------*- C++ -*-===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLD_ELF_OUTPUT_SECTIONS_H
11#define LLD_ELF_OUTPUT_SECTIONS_H
12
Davide Italiano85121bb2015-09-25 03:56:11 +000013#include "Config.h"
Rafael Espindola5616adf2017-03-08 22:36:28 +000014#include "InputSection.h"
Rafael Espindola8c022ca2017-07-27 19:22:43 +000015#include "LinkerScript.h"
Simon Atanasyan41325112016-06-19 21:39:37 +000016#include "Relocations.h"
Davide Italiano85121bb2015-09-25 03:56:11 +000017
Rui Ueyamaa0752a52016-03-13 20:28:29 +000018#include "lld/Core/LLVM.h"
19#include "llvm/MC/StringTableBuilder.h"
20#include "llvm/Object/ELF.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000021
22namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000023namespace elf {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000024
Rafael Espindola17cb7c02016-12-19 17:01:01 +000025struct PhdrEntry;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000026class SymbolBody;
Rafael Espindola2deeb602016-07-21 20:18:30 +000027struct EhSectionPiece;
Rafael Espindola5c02b742017-03-06 21:17:18 +000028class EhInputSection;
Rafael Espindola774ea7d2017-02-23 16:49:07 +000029class InputSection;
Rafael Espindolab4c9b812017-02-23 02:28:28 +000030class InputSectionBase;
Rafael Espindola6119b862017-03-06 20:23:56 +000031class MergeInputSection;
Rafael Espindola24e6f362017-02-24 15:07:30 +000032class OutputSection;
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000033template <class ELFT> class ObjFile;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +000034template <class ELFT> class SharedFile;
Rui Ueyama4076fa12017-02-26 23:35:34 +000035class SharedSymbol;
Rui Ueyama80474a22017-02-28 19:29:55 +000036class DefinedRegular;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000037
Rafael Espindola71675852015-09-22 00:16:19 +000038// This represents a section in an output file.
Rafael Espindola24e6f362017-02-24 15:07:30 +000039// It is composed of multiple InputSections.
Rafael Espindola71675852015-09-22 00:16:19 +000040// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000041// non-overlapping file offsets and VAs.
Rafael Espindola8c022ca2017-07-27 19:22:43 +000042class OutputSection final : public BaseCommand, public SectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000043public:
Rafael Espindola24e6f362017-02-24 15:07:30 +000044 OutputSection(StringRef Name, uint32_t Type, uint64_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000045
Rafael Espindola5616adf2017-03-08 22:36:28 +000046 static bool classof(const SectionBase *S) {
47 return S->kind() == SectionBase::Output;
48 }
Rafael Espindola8c022ca2017-07-27 19:22:43 +000049 static bool classof(const BaseCommand *C);
Rafael Espindola5616adf2017-03-08 22:36:28 +000050
Rafael Espindolae08e78d2016-11-09 23:23:45 +000051 uint64_t getLMA() const { return Addr + LMAOffset; }
52 template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000053
Rui Ueyama2317d0d2015-10-15 20:55:22 +000054 unsigned SectionIndex;
Rafael Espindola52101412017-05-12 14:52:22 +000055 unsigned SortRank;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000056
Rafael Espindola0b113672016-07-27 14:10:56 +000057 uint32_t getPhdrFlags() const;
Rui Ueyama3b04d832016-07-14 05:46:24 +000058
Rafael Espindola8bb40872017-03-07 15:51:09 +000059 void updateAlignment(uint32_t Val) {
Rafael Espindola37707632017-03-07 14:55:52 +000060 if (Val > Alignment)
61 Alignment = Val;
Rafael Espindola115f0f32015-11-03 14:13:40 +000062 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000063
George Rimar582ede82017-09-07 10:53:07 +000064 // Pointer to the PT_LOAD segment, which this section resides in. This field
65 // is used to correctly compute file offset of a section. When two sections
66 // share the same load segment, difference between their file offsets should
67 // be equal to difference between their virtual addresses. To compute some
68 // section offset we use the following formula: Off = Off_first + VA -
69 // VA_first, where Off_first and VA_first is file offset and VA of first
70 // section in PT_LOAD.
71 PhdrEntry *PtLoad = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000072
George Rimar990c9cb2017-06-07 09:20:35 +000073 // Pointer to a relocation section for this section. Usually nullptr because
74 // we consume relocations, but if --emit-relocs is specified (which is rare),
75 // it may have a non-null value.
76 OutputSection *RelocationSection = nullptr;
77
Rafael Espindola04a2e342016-11-09 01:42:41 +000078 // The following fields correspond to Elf_Shdr members.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000079 uint64_t Size = 0;
Rafael Espindolae08e78d2016-11-09 23:23:45 +000080 uint64_t Offset = 0;
Rafael Espindolae08e78d2016-11-09 23:23:45 +000081 uint64_t LMAOffset = 0;
82 uint64_t Addr = 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +000083 uint32_t ShName = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000084
Rafael Espindoladc8eb812017-04-07 01:40:21 +000085 void addSection(InputSection *S);
Eugene Leviant84569e62016-11-29 08:05:44 +000086
George Rimardbf93392017-04-17 08:58:12 +000087 // Used for implementation of --compress-debug-sections option.
Rui Ueyama07c62c12017-04-19 11:32:13 +000088 std::vector<uint8_t> ZDebugHeader;
George Rimardbf93392017-04-17 08:58:12 +000089 llvm::SmallVector<char, 1> CompressedData;
George Rimardbf93392017-04-17 08:58:12 +000090
Eugene Leviant84569e62016-11-29 08:05:44 +000091 // Location in the output buffer.
92 uint8_t *Loc = nullptr;
Rafael Espindola8c022ca2017-07-27 19:22:43 +000093
94 // The following members are normally only used in linker scripts.
95 MemoryRegion *MemRegion = nullptr;
96 Expr AddrExpr;
97 Expr AlignExpr;
98 Expr LMAExpr;
99 Expr SubalignExpr;
100 std::vector<BaseCommand *> Commands;
101 std::vector<StringRef> Phdrs;
102 llvm::Optional<uint32_t> Filler;
103 ConstraintKind Constraint = ConstraintKind::NoConstraint;
104 std::string Location;
105 std::string MemoryRegionName;
106 bool Noload = false;
107
108 template <class ELFT> void finalize();
109 template <class ELFT> void writeTo(uint8_t *Buf);
110 template <class ELFT> void maybeCompress();
111 uint32_t getFiller();
112
113 void sort(std::function<int(InputSectionBase *S)> Order);
114 void sortInitFini();
115 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000116};
117
Rafael Espindola8c022ca2017-07-27 19:22:43 +0000118int getPriority(StringRef S);
119
Rui Ueyama9d1bacb12017-02-27 02:31:26 +0000120// All output sections that are handled by the linker specially are
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000121// globally accessible. Writer initializes them, so don't use them
122// until Writer is initialized.
Rui Ueyama9d1bacb12017-02-27 02:31:26 +0000123struct Out {
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000124 static uint8_t First;
Rafael Espindola24e6f362017-02-24 15:07:30 +0000125 static OutputSection *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000126 static uint8_t *OpdBuf;
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000127 static PhdrEntry *TlsPhdr;
Rafael Espindola24e6f362017-02-24 15:07:30 +0000128 static OutputSection *DebugInfo;
129 static OutputSection *ElfHeader;
130 static OutputSection *ProgramHeaders;
131 static OutputSection *PreinitArray;
132 static OutputSection *InitArray;
133 static OutputSection *FiniArray;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000134};
Rui Ueyamad888d102015-10-09 19:34:55 +0000135
Rafael Espindola72447082017-01-05 14:35:41 +0000136struct SectionKey {
George Rimar6892afa2016-07-12 09:49:43 +0000137 StringRef Name;
Rafael Espindola72447082017-01-05 14:35:41 +0000138 uint64_t Flags;
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000139 uint32_t Alignment;
George Rimar6892afa2016-07-12 09:49:43 +0000140};
George Rimar67c60722017-07-18 11:55:35 +0000141} // namespace elf
142} // namespace lld
Rafael Espindola63866282017-02-16 19:23:15 +0000143namespace llvm {
144template <> struct DenseMapInfo<lld::elf::SectionKey> {
145 static lld::elf::SectionKey getEmptyKey();
146 static lld::elf::SectionKey getTombstoneKey();
147 static unsigned getHashValue(const lld::elf::SectionKey &Val);
148 static bool isEqual(const lld::elf::SectionKey &LHS,
149 const lld::elf::SectionKey &RHS);
150};
George Rimar67c60722017-07-18 11:55:35 +0000151} // namespace llvm
Rafael Espindola63866282017-02-16 19:23:15 +0000152namespace lld {
153namespace elf {
George Rimar6892afa2016-07-12 09:49:43 +0000154
155// This class knows how to create an output section for a given
156// input section. Output section type is determined by various
157// factors, including input section's sh_flags, sh_type and
158// linker scripts.
Rui Ueyama02a036f2017-02-27 02:31:48 +0000159class OutputSectionFactory {
George Rimar6892afa2016-07-12 09:49:43 +0000160public:
Rafael Espindola05531242017-07-06 16:40:44 +0000161 OutputSectionFactory();
Rafael Espindolabd3ab092017-01-05 14:52:46 +0000162 ~OutputSectionFactory();
Rui Ueyama02a036f2017-02-27 02:31:48 +0000163
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000164 void addInputSec(InputSectionBase *IS, StringRef OutsecName);
Rafael Espindola4f013bb2017-04-26 22:30:15 +0000165 void addInputSec(InputSectionBase *IS, StringRef OutsecName,
166 OutputSection *&Sec);
Rafael Espindola82902742017-02-16 17:32:26 +0000167
George Rimar6892afa2016-07-12 09:49:43 +0000168private:
Rafael Espindola24e6f362017-02-24 15:07:30 +0000169 llvm::SmallDenseMap<SectionKey, OutputSection *> Map;
George Rimar6892afa2016-07-12 09:49:43 +0000170};
171
George Rimar78aa2702017-03-13 14:40:58 +0000172uint64_t getHeaderSize();
Rafael Espindolae39709b2017-05-30 20:40:03 +0000173void reportDiscarded(InputSectionBase *IS);
George Rimard6bcde32017-08-04 10:25:29 +0000174void sortByOrder(llvm::MutableArrayRef<InputSection *> In,
175 std::function<int(InputSectionBase *S)> Order);
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000176
Rafael Espindolaf51c8052017-06-13 23:26:31 +0000177extern std::vector<OutputSection *> OutputSections;
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000178} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000179} // namespace lld
180
George Rimar6892afa2016-07-12 09:49:43 +0000181#endif