blob: e421123f181093e17ab4e78ea47074be06a62650 [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 Ueyama3f851702017-10-02 21:00:41 +000018#include "lld/Common/LLVM.h"
Rui Ueyamaa0752a52016-03-13 20:28:29 +000019#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;
Rui Ueyamaf52496e2017-11-03 21:21:47 +000026class Symbol;
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 }
Rui Ueyama5908c2f2017-10-11 02:28:28 +000049
Rafael Espindola8c022ca2017-07-27 19:22:43 +000050 static bool classof(const BaseCommand *C);
Rafael Espindola5616adf2017-03-08 22:36:28 +000051
Rafael Espindolae08e78d2016-11-09 23:23:45 +000052 uint64_t getLMA() const { return Addr + LMAOffset; }
53 template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000054
Rui Ueyama2317d0d2015-10-15 20:55:22 +000055 unsigned SectionIndex;
Rafael Espindola52101412017-05-12 14:52:22 +000056 unsigned SortRank;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000057
Rafael Espindola0b113672016-07-27 14:10:56 +000058 uint32_t getPhdrFlags() const;
Rui Ueyama3b04d832016-07-14 05:46:24 +000059
George Rimar582ede82017-09-07 10:53:07 +000060 // Pointer to the PT_LOAD segment, which this section resides in. This field
61 // is used to correctly compute file offset of a section. When two sections
62 // share the same load segment, difference between their file offsets should
63 // be equal to difference between their virtual addresses. To compute some
64 // section offset we use the following formula: Off = Off_first + VA -
65 // VA_first, where Off_first and VA_first is file offset and VA of first
66 // section in PT_LOAD.
67 PhdrEntry *PtLoad = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000068
George Rimar990c9cb2017-06-07 09:20:35 +000069 // Pointer to a relocation section for this section. Usually nullptr because
70 // we consume relocations, but if --emit-relocs is specified (which is rare),
71 // it may have a non-null value.
72 OutputSection *RelocationSection = nullptr;
73
Rafael Espindola04a2e342016-11-09 01:42:41 +000074 // The following fields correspond to Elf_Shdr members.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000075 uint64_t Size = 0;
Rafael Espindolae08e78d2016-11-09 23:23:45 +000076 uint64_t Offset = 0;
Rafael Espindolae08e78d2016-11-09 23:23:45 +000077 uint64_t LMAOffset = 0;
78 uint64_t Addr = 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +000079 uint32_t ShName = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000080
Rui Ueyama0e2bfb12017-10-07 00:43:31 +000081 void addSection(InputSection *IS);
Eugene Leviant84569e62016-11-29 08:05:44 +000082
83 // Location in the output buffer.
84 uint8_t *Loc = nullptr;
Rafael Espindola8c022ca2017-07-27 19:22:43 +000085
86 // The following members are normally only used in linker scripts.
87 MemoryRegion *MemRegion = nullptr;
88 Expr AddrExpr;
89 Expr AlignExpr;
90 Expr LMAExpr;
91 Expr SubalignExpr;
Rui Ueyama6b394ca2017-10-11 01:50:56 +000092 std::vector<BaseCommand *> SectionCommands;
Rafael Espindola8c022ca2017-07-27 19:22:43 +000093 std::vector<StringRef> Phdrs;
94 llvm::Optional<uint32_t> Filler;
95 ConstraintKind Constraint = ConstraintKind::NoConstraint;
96 std::string Location;
97 std::string MemoryRegionName;
98 bool Noload = false;
99
100 template <class ELFT> void finalize();
101 template <class ELFT> void writeTo(uint8_t *Buf);
102 template <class ELFT> void maybeCompress();
Rafael Espindola8c022ca2017-07-27 19:22:43 +0000103
104 void sort(std::function<int(InputSectionBase *S)> Order);
105 void sortInitFini();
106 void sortCtorsDtors();
Rui Ueyama40bd97a2017-10-06 21:42:37 +0000107
108private:
109 // Used for implementation of --compress-debug-sections option.
110 std::vector<uint8_t> ZDebugHeader;
111 llvm::SmallVector<char, 1> CompressedData;
112
113 uint32_t getFiller();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000114};
115
Rafael Espindola8c022ca2017-07-27 19:22:43 +0000116int getPriority(StringRef S);
117
Rui Ueyama9d1bacb12017-02-27 02:31:26 +0000118// All output sections that are handled by the linker specially are
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000119// globally accessible. Writer initializes them, so don't use them
120// until Writer is initialized.
Rui Ueyama9d1bacb12017-02-27 02:31:26 +0000121struct Out {
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000122 static uint8_t First;
Rafael Espindola24e6f362017-02-24 15:07:30 +0000123 static OutputSection *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000124 static uint8_t *OpdBuf;
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000125 static PhdrEntry *TlsPhdr;
Rafael Espindola24e6f362017-02-24 15:07:30 +0000126 static OutputSection *DebugInfo;
127 static OutputSection *ElfHeader;
128 static OutputSection *ProgramHeaders;
129 static OutputSection *PreinitArray;
130 static OutputSection *InitArray;
131 static OutputSection *FiniArray;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000132};
Rui Ueyamad888d102015-10-09 19:34:55 +0000133
George Rimar67c60722017-07-18 11:55:35 +0000134} // namespace elf
135} // namespace lld
Rui Ueyama40bd97a2017-10-06 21:42:37 +0000136
Rafael Espindola63866282017-02-16 19:23:15 +0000137namespace lld {
138namespace elf {
George Rimar6892afa2016-07-12 09:49:43 +0000139// This class knows how to create an output section for a given
140// input section. Output section type is determined by various
141// factors, including input section's sh_flags, sh_type and
142// linker scripts.
Rui Ueyama02a036f2017-02-27 02:31:48 +0000143class OutputSectionFactory {
George Rimar6892afa2016-07-12 09:49:43 +0000144public:
Rafael Espindola05531242017-07-06 16:40:44 +0000145 OutputSectionFactory();
Rafael Espindolabd3ab092017-01-05 14:52:46 +0000146 ~OutputSectionFactory();
Rui Ueyama02a036f2017-02-27 02:31:48 +0000147
Rui Ueyamac54d5b12017-10-06 23:34:43 +0000148 OutputSection *addInputSec(InputSectionBase *IS, StringRef OutsecName);
Rafael Espindola82902742017-02-16 17:32:26 +0000149
George Rimar6892afa2016-07-12 09:49:43 +0000150private:
George Rimar343e8222017-11-04 09:11:27 +0000151 llvm::StringMap<OutputSection *> Map;
George Rimar6892afa2016-07-12 09:49:43 +0000152};
153
George Rimar78aa2702017-03-13 14:40:58 +0000154uint64_t getHeaderSize();
George Rimard6bcde32017-08-04 10:25:29 +0000155void sortByOrder(llvm::MutableArrayRef<InputSection *> In,
156 std::function<int(InputSectionBase *S)> Order);
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000157
Rafael Espindolaf51c8052017-06-13 23:26:31 +0000158extern std::vector<OutputSection *> OutputSections;
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000159} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000160} // namespace lld
161
George Rimar6892afa2016-07-12 09:49:43 +0000162#endif