blob: ba8216527f3b1c0558cc5e47708969744b3a2930 [file] [log] [blame]
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001//===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ObjectFileELF.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include <algorithm>
Kate Stoneb9c1b512016-09-06 20:57:50 +000013#include <cassert>
Tamas Berghammer9fa11472015-10-27 10:43:27 +000014#include <unordered_map>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015
Stephen Wilsonf325ba92010-07-13 23:07:23 +000016#include "lldb/Core/FileSpecList.h"
Jim Ingham672e6f52011-03-07 23:44:08 +000017#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000018#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/PluginManager.h"
20#include "lldb/Core/Section.h"
Jonas Devlieghere59b78bc2018-11-01 04:45:28 +000021#include "lldb/Host/FileSystem.h"
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +000022#include "lldb/Symbol/DWARFCallFrameInfo.h"
Jim Ingham672e6f52011-03-07 23:44:08 +000023#include "lldb/Symbol/SymbolContext.h"
Steve Pucci9e02dac2014-02-06 19:02:19 +000024#include "lldb/Target/SectionLoadList.h"
Ed Maste54803652013-10-11 17:39:07 +000025#include "lldb/Target/Target.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000026#include "lldb/Utility/ArchSpec.h"
Pavel Labathe2867bc2017-12-15 14:23:58 +000027#include "lldb/Utility/DataBufferHeap.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000028#include "lldb/Utility/Log.h"
Zachary Turner97206d52017-05-12 04:51:55 +000029#include "lldb/Utility/Status.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000030#include "lldb/Utility/Stream.h"
Pavel Labath38d06322017-06-29 14:32:17 +000031#include "lldb/Utility/Timer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
Stephen Wilson499b40e2011-03-30 16:07:05 +000033#include "llvm/ADT/PointerUnion.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000034#include "llvm/ADT/StringRef.h"
Pavel Labathe2867bc2017-12-15 14:23:58 +000035#include "llvm/Object/Decompressor.h"
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +000036#include "llvm/Support/ARMBuildAttributes.h"
Zachary Turner736d4d82014-06-25 05:42:32 +000037#include "llvm/Support/MathExtras.h"
Zachary Turner3f4a4b32017-02-24 18:56:49 +000038#include "llvm/Support/MemoryBuffer.h"
Sagar Thakurad5b55a2016-05-24 14:52:50 +000039#include "llvm/Support/MipsABIFlags.h"
Stephen Wilson499b40e2011-03-30 16:07:05 +000040
Kate Stoneb9c1b512016-09-06 20:57:50 +000041#define CASE_AND_STREAM(s, def, width) \
42 case def: \
43 s->Printf("%-*s", width, #def); \
44 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046using namespace lldb;
47using namespace lldb_private;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000048using namespace elf;
49using namespace llvm::ELF;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050
Stephen Wilson499b40e2011-03-30 16:07:05 +000051namespace {
Todd Fialab91de782014-06-27 16:52:49 +000052
53// ELF note owner definitions
54const char *const LLDB_NT_OWNER_FREEBSD = "FreeBSD";
Kate Stoneb9c1b512016-09-06 20:57:50 +000055const char *const LLDB_NT_OWNER_GNU = "GNU";
56const char *const LLDB_NT_OWNER_NETBSD = "NetBSD";
Kamil Rytarowski12801f12017-03-26 15:34:57 +000057const char *const LLDB_NT_OWNER_OPENBSD = "OpenBSD";
Kate Stoneb9c1b512016-09-06 20:57:50 +000058const char *const LLDB_NT_OWNER_CSR = "csr";
Tamas Berghammerdb037d92015-03-18 10:36:27 +000059const char *const LLDB_NT_OWNER_ANDROID = "Android";
Kate Stoneb9c1b512016-09-06 20:57:50 +000060const char *const LLDB_NT_OWNER_CORE = "CORE";
61const char *const LLDB_NT_OWNER_LINUX = "LINUX";
Todd Fialab91de782014-06-27 16:52:49 +000062
63// ELF note type definitions
Kate Stoneb9c1b512016-09-06 20:57:50 +000064const elf_word LLDB_NT_FREEBSD_ABI_TAG = 0x01;
Todd Fialab91de782014-06-27 16:52:49 +000065const elf_word LLDB_NT_FREEBSD_ABI_SIZE = 4;
66
Kate Stoneb9c1b512016-09-06 20:57:50 +000067const elf_word LLDB_NT_GNU_ABI_TAG = 0x01;
68const elf_word LLDB_NT_GNU_ABI_SIZE = 16;
Todd Fialab91de782014-06-27 16:52:49 +000069
70const elf_word LLDB_NT_GNU_BUILD_ID_TAG = 0x03;
71
Kate Stoneb9c1b512016-09-06 20:57:50 +000072const elf_word LLDB_NT_NETBSD_ABI_TAG = 0x01;
73const elf_word LLDB_NT_NETBSD_ABI_SIZE = 4;
Todd Fialab91de782014-06-27 16:52:49 +000074
75// GNU ABI note OS constants
Kate Stoneb9c1b512016-09-06 20:57:50 +000076const elf_word LLDB_NT_GNU_ABI_OS_LINUX = 0x00;
77const elf_word LLDB_NT_GNU_ABI_OS_HURD = 0x01;
Todd Fialab91de782014-06-27 16:52:49 +000078const elf_word LLDB_NT_GNU_ABI_OS_SOLARIS = 0x02;
79
Greg Claytonb704b692015-10-28 18:04:38 +000080// LLDB_NT_OWNER_CORE and LLDB_NT_OWNER_LINUX note contants
Kate Stoneb9c1b512016-09-06 20:57:50 +000081#define NT_PRSTATUS 1
82#define NT_PRFPREG 2
83#define NT_PRPSINFO 3
84#define NT_TASKSTRUCT 4
85#define NT_AUXV 6
86#define NT_SIGINFO 0x53494749
87#define NT_FILE 0x46494c45
88#define NT_PRXFPREG 0x46e62b7f
89#define NT_PPC_VMX 0x100
90#define NT_PPC_SPE 0x101
91#define NT_PPC_VSX 0x102
92#define NT_386_TLS 0x200
93#define NT_386_IOPERM 0x201
94#define NT_X86_XSTATE 0x202
95#define NT_S390_HIGH_GPRS 0x300
96#define NT_S390_TIMER 0x301
97#define NT_S390_TODCMP 0x302
98#define NT_S390_TODPREG 0x303
99#define NT_S390_CTRS 0x304
100#define NT_S390_PREFIX 0x305
101#define NT_S390_LAST_BREAK 0x306
102#define NT_S390_SYSTEM_CALL 0x307
103#define NT_S390_TDB 0x308
104#define NT_S390_VXRS_LOW 0x309
105#define NT_S390_VXRS_HIGH 0x30a
106#define NT_ARM_VFP 0x400
107#define NT_ARM_TLS 0x401
108#define NT_ARM_HW_BREAK 0x402
109#define NT_ARM_HW_WATCH 0x403
110#define NT_ARM_SYSTEM_CALL 0x404
111#define NT_METAG_CBUF 0x500
112#define NT_METAG_RPIPE 0x501
113#define NT_METAG_TLS 0x502
Greg Claytonb704b692015-10-28 18:04:38 +0000114
Stephen Wilson499b40e2011-03-30 16:07:05 +0000115//===----------------------------------------------------------------------===//
116/// @class ELFRelocation
Adrian Prantld8f460e2018-05-02 16:55:16 +0000117/// Generic wrapper for ELFRel and ELFRela.
Stephen Wilson499b40e2011-03-30 16:07:05 +0000118///
119/// This helper class allows us to parse both ELFRel and ELFRela relocation
120/// entries in a generic manner.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121class ELFRelocation {
Stephen Wilson499b40e2011-03-30 16:07:05 +0000122public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 /// Constructs an ELFRelocation entry with a personality as given by @p
124 /// type.
125 ///
126 /// @param type Either DT_REL or DT_RELA. Any other value is invalid.
127 ELFRelocation(unsigned type);
Stephen Wilson499b40e2011-03-30 16:07:05 +0000128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 ~ELFRelocation();
Ed Maste81b4c5f2016-01-04 01:43:47 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilson499b40e2011-03-30 16:07:05 +0000132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 static unsigned RelocType32(const ELFRelocation &rel);
Stephen Wilson499b40e2011-03-30 16:07:05 +0000134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 static unsigned RelocType64(const ELFRelocation &rel);
Stephen Wilson499b40e2011-03-30 16:07:05 +0000136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 static unsigned RelocSymbol32(const ELFRelocation &rel);
Stephen Wilson499b40e2011-03-30 16:07:05 +0000138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 static unsigned RelocSymbol64(const ELFRelocation &rel);
Stephen Wilson499b40e2011-03-30 16:07:05 +0000140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 static unsigned RelocOffset32(const ELFRelocation &rel);
Stephen Wilson499b40e2011-03-30 16:07:05 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 static unsigned RelocOffset64(const ELFRelocation &rel);
Andrew MacPherson17220c12014-03-05 10:12:43 +0000144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 static unsigned RelocAddend32(const ELFRelocation &rel);
Andrew MacPherson17220c12014-03-05 10:12:43 +0000146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147 static unsigned RelocAddend64(const ELFRelocation &rel);
Andrew MacPherson17220c12014-03-05 10:12:43 +0000148
Stephen Wilson499b40e2011-03-30 16:07:05 +0000149private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 typedef llvm::PointerUnion<ELFRel *, ELFRela *> RelocUnion;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000151
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 RelocUnion reloc;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000153};
154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155ELFRelocation::ELFRelocation(unsigned type) {
156 if (type == DT_REL || type == SHT_REL)
157 reloc = new ELFRel();
158 else if (type == DT_RELA || type == SHT_RELA)
159 reloc = new ELFRela();
160 else {
161 assert(false && "unexpected relocation type");
162 reloc = static_cast<ELFRel *>(NULL);
163 }
Stephen Wilson499b40e2011-03-30 16:07:05 +0000164}
165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166ELFRelocation::~ELFRelocation() {
167 if (reloc.is<ELFRel *>())
168 delete reloc.get<ELFRel *>();
169 else
170 delete reloc.get<ELFRela *>();
Stephen Wilson499b40e2011-03-30 16:07:05 +0000171}
172
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173bool ELFRelocation::Parse(const lldb_private::DataExtractor &data,
174 lldb::offset_t *offset) {
175 if (reloc.is<ELFRel *>())
176 return reloc.get<ELFRel *>()->Parse(data, offset);
177 else
178 return reloc.get<ELFRela *>()->Parse(data, offset);
Stephen Wilson499b40e2011-03-30 16:07:05 +0000179}
180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181unsigned ELFRelocation::RelocType32(const ELFRelocation &rel) {
182 if (rel.reloc.is<ELFRel *>())
183 return ELFRel::RelocType32(*rel.reloc.get<ELFRel *>());
184 else
185 return ELFRela::RelocType32(*rel.reloc.get<ELFRela *>());
Stephen Wilson499b40e2011-03-30 16:07:05 +0000186}
187
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188unsigned ELFRelocation::RelocType64(const ELFRelocation &rel) {
189 if (rel.reloc.is<ELFRel *>())
190 return ELFRel::RelocType64(*rel.reloc.get<ELFRel *>());
191 else
192 return ELFRela::RelocType64(*rel.reloc.get<ELFRela *>());
Stephen Wilson499b40e2011-03-30 16:07:05 +0000193}
194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195unsigned ELFRelocation::RelocSymbol32(const ELFRelocation &rel) {
196 if (rel.reloc.is<ELFRel *>())
197 return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel *>());
198 else
199 return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela *>());
Stephen Wilson499b40e2011-03-30 16:07:05 +0000200}
201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202unsigned ELFRelocation::RelocSymbol64(const ELFRelocation &rel) {
203 if (rel.reloc.is<ELFRel *>())
204 return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel *>());
205 else
206 return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela *>());
Stephen Wilson499b40e2011-03-30 16:07:05 +0000207}
208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209unsigned ELFRelocation::RelocOffset32(const ELFRelocation &rel) {
210 if (rel.reloc.is<ELFRel *>())
211 return rel.reloc.get<ELFRel *>()->r_offset;
212 else
213 return rel.reloc.get<ELFRela *>()->r_offset;
Andrew MacPherson17220c12014-03-05 10:12:43 +0000214}
215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216unsigned ELFRelocation::RelocOffset64(const ELFRelocation &rel) {
217 if (rel.reloc.is<ELFRel *>())
218 return rel.reloc.get<ELFRel *>()->r_offset;
219 else
220 return rel.reloc.get<ELFRela *>()->r_offset;
Andrew MacPherson17220c12014-03-05 10:12:43 +0000221}
222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223unsigned ELFRelocation::RelocAddend32(const ELFRelocation &rel) {
224 if (rel.reloc.is<ELFRel *>())
225 return 0;
226 else
227 return rel.reloc.get<ELFRela *>()->r_addend;
Andrew MacPherson17220c12014-03-05 10:12:43 +0000228}
229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230unsigned ELFRelocation::RelocAddend64(const ELFRelocation &rel) {
231 if (rel.reloc.is<ELFRel *>())
232 return 0;
233 else
234 return rel.reloc.get<ELFRela *>()->r_addend;
Andrew MacPherson17220c12014-03-05 10:12:43 +0000235}
236
Stephen Wilson499b40e2011-03-30 16:07:05 +0000237} // end anonymous namespace
238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239bool ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset) {
240 // Read all fields.
241 if (data.GetU32(offset, &n_namesz, 3) == NULL)
242 return false;
Ed Mastec113ff82013-12-02 17:49:13 +0000243
Adrian Prantl05097242018-04-30 16:49:04 +0000244 // The name field is required to be nul-terminated, and n_namesz includes the
245 // terminating nul in observed implementations (contrary to the ELF-64 spec).
246 // A special case is needed for cores generated by some older Linux versions,
247 // which write a note named "CORE" without a nul terminator and n_namesz = 4.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 if (n_namesz == 4) {
249 char buf[4];
250 if (data.ExtractBytes(*offset, 4, data.GetByteOrder(), buf) != 4)
251 return false;
252 if (strncmp(buf, "CORE", 4) == 0) {
253 n_name = "CORE";
254 *offset += 4;
255 return true;
Ed Mastec113ff82013-12-02 17:49:13 +0000256 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 }
Ed Mastec113ff82013-12-02 17:49:13 +0000258
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 const char *cstr = data.GetCStr(offset, llvm::alignTo(n_namesz, 4));
260 if (cstr == NULL) {
261 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS));
262 if (log)
263 log->Printf("Failed to parse note name lacking nul terminator");
Ed Mastec113ff82013-12-02 17:49:13 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 return false;
266 }
267 n_name = cstr;
268 return true;
Ed Mastec113ff82013-12-02 17:49:13 +0000269}
270
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271static uint32_t kalimbaVariantFromElfFlags(const elf::elf_word e_flags) {
272 const uint32_t dsp_rev = e_flags & 0xFF;
273 uint32_t kal_arch_variant = LLDB_INVALID_CPUTYPE;
274 switch (dsp_rev) {
275 // TODO(mg11) Support more variants
276 case 10:
277 kal_arch_variant = llvm::Triple::KalimbaSubArch_v3;
278 break;
279 case 14:
280 kal_arch_variant = llvm::Triple::KalimbaSubArch_v4;
281 break;
282 case 17:
283 case 20:
284 kal_arch_variant = llvm::Triple::KalimbaSubArch_v5;
285 break;
286 default:
287 break;
288 }
289 return kal_arch_variant;
Matthew Gardiner5f675792014-08-27 12:09:39 +0000290}
291
Nitesh Jain706c5202017-03-31 11:06:25 +0000292static uint32_t mipsVariantFromElfFlags (const elf::ELFHeader &header) {
293 const uint32_t mips_arch = header.e_flags & llvm::ELF::EF_MIPS_ARCH;
294 uint32_t endian = header.e_ident[EI_DATA];
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 uint32_t arch_variant = ArchSpec::eMIPSSubType_unknown;
Nitesh Jain706c5202017-03-31 11:06:25 +0000296 uint32_t fileclass = header.e_ident[EI_CLASS];
297
Adrian Prantl05097242018-04-30 16:49:04 +0000298 // If there aren't any elf flags available (e.g core elf file) then return
299 // default
Nitesh Jain706c5202017-03-31 11:06:25 +0000300 // 32 or 64 bit arch (without any architecture revision) based on object file's class.
301 if (header.e_type == ET_CORE) {
302 switch (fileclass) {
303 case llvm::ELF::ELFCLASS32:
304 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el
305 : ArchSpec::eMIPSSubType_mips32;
306 case llvm::ELF::ELFCLASS64:
307 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el
308 : ArchSpec::eMIPSSubType_mips64;
309 default:
310 return arch_variant;
311 }
312 }
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +0000313
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 switch (mips_arch) {
315 case llvm::ELF::EF_MIPS_ARCH_1:
316 case llvm::ELF::EF_MIPS_ARCH_2:
317 case llvm::ELF::EF_MIPS_ARCH_32:
318 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el
319 : ArchSpec::eMIPSSubType_mips32;
320 case llvm::ELF::EF_MIPS_ARCH_32R2:
321 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r2el
322 : ArchSpec::eMIPSSubType_mips32r2;
323 case llvm::ELF::EF_MIPS_ARCH_32R6:
324 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r6el
325 : ArchSpec::eMIPSSubType_mips32r6;
326 case llvm::ELF::EF_MIPS_ARCH_3:
327 case llvm::ELF::EF_MIPS_ARCH_4:
328 case llvm::ELF::EF_MIPS_ARCH_5:
329 case llvm::ELF::EF_MIPS_ARCH_64:
330 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el
331 : ArchSpec::eMIPSSubType_mips64;
332 case llvm::ELF::EF_MIPS_ARCH_64R2:
333 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r2el
334 : ArchSpec::eMIPSSubType_mips64r2;
335 case llvm::ELF::EF_MIPS_ARCH_64R6:
336 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r6el
337 : ArchSpec::eMIPSSubType_mips64r6;
338 default:
339 break;
340 }
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +0000341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 return arch_variant;
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +0000343}
344
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345static uint32_t subTypeFromElfHeader(const elf::ELFHeader &header) {
346 if (header.e_machine == llvm::ELF::EM_MIPS)
Nitesh Jain706c5202017-03-31 11:06:25 +0000347 return mipsVariantFromElfFlags(header);
Mohit K. Bhakkad3df471c2015-03-17 11:43:56 +0000348
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 return llvm::ELF::EM_CSR_KALIMBA == header.e_machine
350 ? kalimbaVariantFromElfFlags(header.e_flags)
351 : LLDB_INVALID_CPUTYPE;
Matthew Gardiner5f675792014-08-27 12:09:39 +0000352}
353
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000354//! The kalimba toolchain identifies a code section as being
355//! one with the SHT_PROGBITS set in the section sh_type and the top
356//! bit in the 32-bit address field set.
357static lldb::SectionType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358kalimbaSectionType(const elf::ELFHeader &header,
359 const elf::ELFSectionHeader &sect_hdr) {
360 if (llvm::ELF::EM_CSR_KALIMBA != header.e_machine) {
Matthew Gardiner6e7b0a02014-10-15 08:21:54 +0000361 return eSectionTypeOther;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 }
363
364 if (llvm::ELF::SHT_NOBITS == sect_hdr.sh_type) {
365 return eSectionTypeZeroFill;
366 }
367
368 if (llvm::ELF::SHT_PROGBITS == sect_hdr.sh_type) {
369 const lldb::addr_t KAL_CODE_BIT = 1 << 31;
370 return KAL_CODE_BIT & sect_hdr.sh_addr ? eSectionTypeCode
371 : eSectionTypeData;
372 }
373
374 return eSectionTypeOther;
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000375}
376
Todd Fiala4339f3a2014-03-25 19:29:09 +0000377// Arbitrary constant used as UUID prefix for core files.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378const uint32_t ObjectFileELF::g_core_uuid_magic(0xE210C);
Todd Fiala4339f3a2014-03-25 19:29:09 +0000379
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000380//------------------------------------------------------------------
381// Static methods.
382//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383void ObjectFileELF::Initialize() {
384 PluginManager::RegisterPlugin(GetPluginNameStatic(),
385 GetPluginDescriptionStatic(), CreateInstance,
386 CreateMemoryInstance, GetModuleSpecifications);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387}
388
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389void ObjectFileELF::Terminate() {
390 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391}
392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393lldb_private::ConstString ObjectFileELF::GetPluginNameStatic() {
394 static ConstString g_name("elf");
395 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396}
397
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398const char *ObjectFileELF::GetPluginDescriptionStatic() {
399 return "ELF object file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400}
401
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402ObjectFile *ObjectFileELF::CreateInstance(const lldb::ModuleSP &module_sp,
403 DataBufferSP &data_sp,
404 lldb::offset_t data_offset,
405 const lldb_private::FileSpec *file,
406 lldb::offset_t file_offset,
407 lldb::offset_t length) {
408 if (!data_sp) {
Pavel Labath50251fc2017-12-21 10:54:30 +0000409 data_sp = MapFileData(*file, length, file_offset);
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000410 if (!data_sp)
411 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 data_offset = 0;
413 }
414
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000415 assert(data_sp);
416
417 if (data_sp->GetByteSize() <= (llvm::ELF::EI_NIDENT + data_offset))
418 return nullptr;
419
420 const uint8_t *magic = data_sp->GetBytes() + data_offset;
421 if (!ELFHeader::MagicBytesMatch(magic))
422 return nullptr;
423
424 // Update the data to contain the entire file if it doesn't already
425 if (data_sp->GetByteSize() < length) {
Pavel Labath50251fc2017-12-21 10:54:30 +0000426 data_sp = MapFileData(*file, length, file_offset);
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000427 if (!data_sp)
428 return nullptr;
429 data_offset = 0;
430 magic = data_sp->GetBytes();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 }
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000432
433 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
434 if (address_size == 4 || address_size == 8) {
435 std::unique_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(
436 module_sp, data_sp, data_offset, file, file_offset, length));
437 ArchSpec spec;
438 if (objfile_ap->GetArchitecture(spec) &&
439 objfile_ap->SetModulesArchitecture(spec))
440 return objfile_ap.release();
441 }
442
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444}
445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446ObjectFile *ObjectFileELF::CreateMemoryInstance(
447 const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
448 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {
449 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT)) {
450 const uint8_t *magic = data_sp->GetBytes();
451 if (ELFHeader::MagicBytesMatch(magic)) {
452 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
453 if (address_size == 4 || address_size == 8) {
Benjamin Kramere1a60742017-09-14 15:01:55 +0000454 std::unique_ptr<ObjectFileELF> objfile_ap(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 new ObjectFileELF(module_sp, data_sp, process_sp, header_addr));
456 ArchSpec spec;
457 if (objfile_ap->GetArchitecture(spec) &&
458 objfile_ap->SetModulesArchitecture(spec))
459 return objfile_ap.release();
460 }
Andrew MacPherson17220c12014-03-05 10:12:43 +0000461 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 }
463 return NULL;
Greg Claytonc9660542012-02-05 02:38:54 +0000464}
465
Kate Stoneb9c1b512016-09-06 20:57:50 +0000466bool ObjectFileELF::MagicBytesMatch(DataBufferSP &data_sp,
467 lldb::addr_t data_offset,
468 lldb::addr_t data_length) {
469 if (data_sp &&
470 data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) {
471 const uint8_t *magic = data_sp->GetBytes() + data_offset;
472 return ELFHeader::MagicBytesMatch(magic);
473 }
474 return false;
Michael Sartain9f0013d2013-05-17 00:20:21 +0000475}
Greg Claytonc9660542012-02-05 02:38:54 +0000476
Michael Sartain9f4517a2013-07-03 01:52:14 +0000477/*
478 * crc function from http://svnweb.freebsd.org/base/head/sys/libkern/crc32.c
479 *
480 * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or
481 * code or tables extracted from it, as desired without restriction.
482 */
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483static uint32_t calc_crc32(uint32_t crc, const void *buf, size_t size) {
484 static const uint32_t g_crc32_tab[] = {
485 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
486 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
487 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
488 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
489 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
490 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
491 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
492 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
493 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
494 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
495 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
496 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
497 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
498 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
499 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
500 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
501 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
502 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
503 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
504 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
505 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
506 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
507 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
508 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
509 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
510 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
511 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
512 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
513 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
514 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
515 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
516 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
517 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
518 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
519 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
520 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
521 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
522 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
523 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
524 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
525 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
526 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
527 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d};
528 const uint8_t *p = (const uint8_t *)buf;
Michael Sartain9f4517a2013-07-03 01:52:14 +0000529
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530 crc = crc ^ ~0U;
531 while (size--)
532 crc = g_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
533 return crc ^ ~0U;
Michael Sartain9f4517a2013-07-03 01:52:14 +0000534}
535
Kate Stoneb9c1b512016-09-06 20:57:50 +0000536static uint32_t calc_gnu_debuglink_crc32(const void *buf, size_t size) {
537 return calc_crc32(0U, buf, size);
Todd Fiala4339f3a2014-03-25 19:29:09 +0000538}
539
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540uint32_t ObjectFileELF::CalculateELFNotesSegmentsCRC32(
541 const ProgramHeaderColl &program_headers, DataExtractor &object_data) {
542 typedef ProgramHeaderCollConstIter Iter;
Todd Fiala4339f3a2014-03-25 19:29:09 +0000543
Kate Stoneb9c1b512016-09-06 20:57:50 +0000544 uint32_t core_notes_crc = 0;
Todd Fiala4339f3a2014-03-25 19:29:09 +0000545
Kate Stoneb9c1b512016-09-06 20:57:50 +0000546 for (Iter I = program_headers.begin(); I != program_headers.end(); ++I) {
547 if (I->p_type == llvm::ELF::PT_NOTE) {
548 const elf_off ph_offset = I->p_offset;
549 const size_t ph_size = I->p_filesz;
Todd Fiala4339f3a2014-03-25 19:29:09 +0000550
Kate Stoneb9c1b512016-09-06 20:57:50 +0000551 DataExtractor segment_data;
552 if (segment_data.SetData(object_data, ph_offset, ph_size) != ph_size) {
Adrian Prantl05097242018-04-30 16:49:04 +0000553 // The ELF program header contained incorrect data, probably corefile
554 // is incomplete or corrupted.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555 break;
556 }
Todd Fiala4339f3a2014-03-25 19:29:09 +0000557
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558 core_notes_crc = calc_crc32(core_notes_crc, segment_data.GetDataStart(),
559 segment_data.GetByteSize());
Todd Fiala4339f3a2014-03-25 19:29:09 +0000560 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561 }
Todd Fiala4339f3a2014-03-25 19:29:09 +0000562
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563 return core_notes_crc;
Todd Fiala4339f3a2014-03-25 19:29:09 +0000564}
565
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566static const char *OSABIAsCString(unsigned char osabi_byte) {
567#define _MAKE_OSABI_CASE(x) \
568 case x: \
569 return #x
570 switch (osabi_byte) {
571 _MAKE_OSABI_CASE(ELFOSABI_NONE);
572 _MAKE_OSABI_CASE(ELFOSABI_HPUX);
573 _MAKE_OSABI_CASE(ELFOSABI_NETBSD);
574 _MAKE_OSABI_CASE(ELFOSABI_GNU);
575 _MAKE_OSABI_CASE(ELFOSABI_HURD);
576 _MAKE_OSABI_CASE(ELFOSABI_SOLARIS);
577 _MAKE_OSABI_CASE(ELFOSABI_AIX);
578 _MAKE_OSABI_CASE(ELFOSABI_IRIX);
579 _MAKE_OSABI_CASE(ELFOSABI_FREEBSD);
580 _MAKE_OSABI_CASE(ELFOSABI_TRU64);
581 _MAKE_OSABI_CASE(ELFOSABI_MODESTO);
582 _MAKE_OSABI_CASE(ELFOSABI_OPENBSD);
583 _MAKE_OSABI_CASE(ELFOSABI_OPENVMS);
584 _MAKE_OSABI_CASE(ELFOSABI_NSK);
585 _MAKE_OSABI_CASE(ELFOSABI_AROS);
586 _MAKE_OSABI_CASE(ELFOSABI_FENIXOS);
587 _MAKE_OSABI_CASE(ELFOSABI_C6000_ELFABI);
588 _MAKE_OSABI_CASE(ELFOSABI_C6000_LINUX);
589 _MAKE_OSABI_CASE(ELFOSABI_ARM);
590 _MAKE_OSABI_CASE(ELFOSABI_STANDALONE);
591 default:
592 return "<unknown-osabi>";
593 }
Todd Fialab91de782014-06-27 16:52:49 +0000594#undef _MAKE_OSABI_CASE
595}
596
Ed Mastef6a13122015-06-05 13:03:08 +0000597//
598// WARNING : This function is being deprecated
Adrian Prantl05097242018-04-30 16:49:04 +0000599// It's functionality has moved to ArchSpec::SetArchitecture This function is
600// only being kept to validate the move.
Ed Mastef6a13122015-06-05 13:03:08 +0000601//
602// TODO : Remove this function
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603static bool GetOsFromOSABI(unsigned char osabi_byte,
604 llvm::Triple::OSType &ostype) {
605 switch (osabi_byte) {
606 case ELFOSABI_AIX:
607 ostype = llvm::Triple::OSType::AIX;
608 break;
609 case ELFOSABI_FREEBSD:
610 ostype = llvm::Triple::OSType::FreeBSD;
611 break;
612 case ELFOSABI_GNU:
613 ostype = llvm::Triple::OSType::Linux;
614 break;
615 case ELFOSABI_NETBSD:
616 ostype = llvm::Triple::OSType::NetBSD;
617 break;
618 case ELFOSABI_OPENBSD:
619 ostype = llvm::Triple::OSType::OpenBSD;
620 break;
621 case ELFOSABI_SOLARIS:
622 ostype = llvm::Triple::OSType::Solaris;
623 break;
624 default:
625 ostype = llvm::Triple::OSType::UnknownOS;
626 }
627 return ostype != llvm::Triple::OSType::UnknownOS;
Todd Fialab91de782014-06-27 16:52:49 +0000628}
629
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630size_t ObjectFileELF::GetModuleSpecifications(
631 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
632 lldb::offset_t data_offset, lldb::offset_t file_offset,
633 lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
634 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES));
Todd Fialab91de782014-06-27 16:52:49 +0000635
Kate Stoneb9c1b512016-09-06 20:57:50 +0000636 const size_t initial_count = specs.GetSize();
Michael Sartainc836ae72013-05-23 20:57:03 +0000637
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638 if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) {
639 DataExtractor data;
640 data.SetData(data_sp);
641 elf::ELFHeader header;
Pavel Labath23ccc292017-01-31 23:09:46 +0000642 lldb::offset_t header_offset = data_offset;
643 if (header.Parse(data, &header_offset)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644 if (data_sp) {
645 ModuleSpec spec(file);
Matthew Gardiner5f675792014-08-27 12:09:39 +0000646
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647 const uint32_t sub_type = subTypeFromElfHeader(header);
648 spec.GetArchitecture().SetArchitecture(
649 eArchTypeELF, header.e_machine, sub_type, header.e_ident[EI_OSABI]);
Matthew Gardiner5f675792014-08-27 12:09:39 +0000650
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651 if (spec.GetArchitecture().IsValid()) {
652 llvm::Triple::OSType ostype;
653 llvm::Triple::VendorType vendor;
654 llvm::Triple::OSType spec_ostype =
655 spec.GetArchitecture().GetTriple().getOS();
Todd Fialab91de782014-06-27 16:52:49 +0000656
Kate Stoneb9c1b512016-09-06 20:57:50 +0000657 if (log)
658 log->Printf("ObjectFileELF::%s file '%s' module OSABI: %s",
659 __FUNCTION__, file.GetPath().c_str(),
660 OSABIAsCString(header.e_ident[EI_OSABI]));
Ed Mastef6a13122015-06-05 13:03:08 +0000661
Kate Stoneb9c1b512016-09-06 20:57:50 +0000662 // SetArchitecture should have set the vendor to unknown
663 vendor = spec.GetArchitecture().GetTriple().getVendor();
664 assert(vendor == llvm::Triple::UnknownVendor);
Hafiz Abid Qadeerb1554312017-01-20 10:24:03 +0000665 UNUSED_IF_ASSERT_DISABLED(vendor);
Ed Mastef6a13122015-06-05 13:03:08 +0000666
Kate Stoneb9c1b512016-09-06 20:57:50 +0000667 //
668 // Validate it is ok to remove GetOsFromOSABI
669 GetOsFromOSABI(header.e_ident[EI_OSABI], ostype);
670 assert(spec_ostype == ostype);
671 if (spec_ostype != llvm::Triple::OSType::UnknownOS) {
672 if (log)
673 log->Printf("ObjectFileELF::%s file '%s' set ELF module OS type "
674 "from ELF header OSABI.",
675 __FUNCTION__, file.GetPath().c_str());
676 }
Michael Sartaina7499c92013-07-01 19:45:50 +0000677
Pavel Labath4f033122018-02-05 17:25:40 +0000678 data_sp = MapFileData(file, -1, file_offset);
679 if (data_sp)
680 data.SetData(data_sp);
Adrian Prantl05097242018-04-30 16:49:04 +0000681 // In case there is header extension in the section #0, the header we
682 // parsed above could have sentinel values for e_phnum, e_shnum, and
683 // e_shstrndx. In this case we need to reparse the header with a
684 // bigger data source to get the actual values.
Pavel Labath4f033122018-02-05 17:25:40 +0000685 if (header.HasHeaderExtension()) {
686 lldb::offset_t header_offset = data_offset;
687 header.Parse(data, &header_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000688 }
Michael Sartaina7499c92013-07-01 19:45:50 +0000689
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690 uint32_t gnu_debuglink_crc = 0;
691 std::string gnu_debuglink_file;
692 SectionHeaderColl section_headers;
693 lldb_private::UUID &uuid = spec.GetUUID();
Michael Sartain9f4517a2013-07-03 01:52:14 +0000694
Pavel Labathdf1a0d12017-06-20 08:11:47 +0000695 GetSectionHeaderInfo(section_headers, data, header, uuid,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696 gnu_debuglink_file, gnu_debuglink_crc,
697 spec.GetArchitecture());
Ravitheja Addepally15f89c42016-01-19 12:55:21 +0000698
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699 llvm::Triple &spec_triple = spec.GetArchitecture().GetTriple();
Todd Fialab91de782014-06-27 16:52:49 +0000700
Kate Stoneb9c1b512016-09-06 20:57:50 +0000701 if (log)
702 log->Printf("ObjectFileELF::%s file '%s' module set to triple: %s "
703 "(architecture %s)",
704 __FUNCTION__, file.GetPath().c_str(),
705 spec_triple.getTriple().c_str(),
706 spec.GetArchitecture().GetArchitectureName());
Todd Fialab91de782014-06-27 16:52:49 +0000707
Kate Stoneb9c1b512016-09-06 20:57:50 +0000708 if (!uuid.IsValid()) {
709 uint32_t core_notes_crc = 0;
Todd Fiala4339f3a2014-03-25 19:29:09 +0000710
Kate Stoneb9c1b512016-09-06 20:57:50 +0000711 if (!gnu_debuglink_crc) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000712 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000713 lldb_private::Timer scoped_timer(
Pavel Labathf9d16472017-05-15 13:02:37 +0000714 func_cat,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715 "Calculating module crc32 %s with size %" PRIu64 " KiB",
716 file.GetLastPathComponent().AsCString(),
Jonas Devlieghere59b78bc2018-11-01 04:45:28 +0000717 (FileSystem::Instance().GetByteSize(file) - file_offset) /
718 1024);
Todd Fiala4339f3a2014-03-25 19:29:09 +0000719
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720 // For core files - which usually don't happen to have a
Zachary Turner3f4a4b32017-02-24 18:56:49 +0000721 // gnu_debuglink, and are pretty bulky - calculating whole
722 // contents crc32 would be too much of luxury. Thus we will need
723 // to fallback to something simpler.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724 if (header.e_type == llvm::ELF::ET_CORE) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000725 ProgramHeaderColl program_headers;
Pavel Labathdf1a0d12017-06-20 08:11:47 +0000726 GetProgramHeaderInfo(program_headers, data, header);
Michael Sartainc836ae72013-05-23 20:57:03 +0000727
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 core_notes_crc =
729 CalculateELFNotesSegmentsCRC32(program_headers, data);
730 } else {
Pavel Labath4f033122018-02-05 17:25:40 +0000731 gnu_debuglink_crc = calc_gnu_debuglink_crc32(
732 data.GetDataStart(), data.GetByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 }
734 }
Pavel Labath77c397f2018-06-29 11:20:29 +0000735 using u32le = llvm::support::ulittle32_t;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 if (gnu_debuglink_crc) {
737 // Use 4 bytes of crc from the .gnu_debuglink section.
Pavel Labath77c397f2018-06-29 11:20:29 +0000738 u32le data(gnu_debuglink_crc);
739 uuid = UUID::fromData(&data, sizeof(data));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000740 } else if (core_notes_crc) {
741 // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make
Adrian Prantl05097242018-04-30 16:49:04 +0000742 // it look different form .gnu_debuglink crc followed by 4 bytes
743 // of note segments crc.
Pavel Labath77c397f2018-06-29 11:20:29 +0000744 u32le data[] = {u32le(g_core_uuid_magic), u32le(core_notes_crc)};
745 uuid = UUID::fromData(data, sizeof(data));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746 }
747 }
748
749 specs.Append(spec);
750 }
751 }
752 }
753 }
754
755 return specs.GetSize() - initial_count;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000756}
757
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000758//------------------------------------------------------------------
759// PluginInterface protocol
760//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761lldb_private::ConstString ObjectFileELF::GetPluginName() {
762 return GetPluginNameStatic();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000763}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765uint32_t ObjectFileELF::GetPluginVersion() { return m_plugin_version; }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000766//------------------------------------------------------------------
767// ObjectFile protocol
768//------------------------------------------------------------------
769
Kate Stoneb9c1b512016-09-06 20:57:50 +0000770ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp,
771 DataBufferSP &data_sp, lldb::offset_t data_offset,
772 const FileSpec *file, lldb::offset_t file_offset,
773 lldb::offset_t length)
774 : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
775 m_header(), m_uuid(), m_gnu_debuglink_file(), m_gnu_debuglink_crc(0),
776 m_program_headers(), m_section_headers(), m_dynamic_symbols(),
777 m_filespec_ap(), m_entry_point_address(), m_arch_spec() {
778 if (file)
779 m_file = *file;
780 ::memset(&m_header, 0, sizeof(m_header));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781}
782
Kate Stoneb9c1b512016-09-06 20:57:50 +0000783ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp,
784 DataBufferSP &header_data_sp,
785 const lldb::ProcessSP &process_sp,
786 addr_t header_addr)
787 : ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
788 m_header(), m_uuid(), m_gnu_debuglink_file(), m_gnu_debuglink_crc(0),
789 m_program_headers(), m_section_headers(), m_dynamic_symbols(),
790 m_filespec_ap(), m_entry_point_address(), m_arch_spec() {
791 ::memset(&m_header, 0, sizeof(m_header));
Andrew MacPherson17220c12014-03-05 10:12:43 +0000792}
793
Kate Stoneb9c1b512016-09-06 20:57:50 +0000794ObjectFileELF::~ObjectFileELF() {}
795
796bool ObjectFileELF::IsExecutable() const {
797 return ((m_header.e_type & ET_EXEC) != 0) || (m_header.e_entry != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000798}
799
Kate Stoneb9c1b512016-09-06 20:57:50 +0000800bool ObjectFileELF::SetLoadAddress(Target &target, lldb::addr_t value,
801 bool value_is_offset) {
802 ModuleSP module_sp = GetModule();
803 if (module_sp) {
804 size_t num_loaded_sections = 0;
805 SectionList *section_list = GetSectionList();
806 if (section_list) {
807 if (!value_is_offset) {
808 bool found_offset = false;
Kamil Rytarowski12801f12017-03-26 15:34:57 +0000809 for (size_t i = 1, count = GetProgramHeaderCount(); i <= count; ++i) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000810 const elf::ELFProgramHeader *header = GetProgramHeaderByIndex(i);
811 if (header == nullptr)
812 continue;
Jim Ingham5aee1622010-08-09 23:31:02 +0000813
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814 if (header->p_type != PT_LOAD || header->p_offset != 0)
815 continue;
Tamas Berghammerf2561842015-06-30 10:41:23 +0000816
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817 value = value - header->p_vaddr;
818 found_offset = true;
819 break;
Steve Pucci9e02dac2014-02-06 19:02:19 +0000820 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000821 if (!found_offset)
822 return false;
823 }
824
825 const size_t num_sections = section_list->GetSize();
826 size_t sect_idx = 0;
827
828 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +0000829 // Iterate through the object file sections to find all of the sections
830 // that have SHF_ALLOC in their flag bits.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000831 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
832 if (section_sp && section_sp->Test(SHF_ALLOC)) {
Pavel Labathec03d7e2018-02-28 20:42:29 +0000833 lldb::addr_t load_addr = section_sp->GetFileAddress();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000834 // We don't want to update the load address of a section with type
835 // eSectionTypeAbsoluteAddress as they already have the absolute load
Adrian Prantl05097242018-04-30 16:49:04 +0000836 // address already specified
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 if (section_sp->GetType() != eSectionTypeAbsoluteAddress)
838 load_addr += value;
839
840 // On 32-bit systems the load address have to fit into 4 bytes. The
Adrian Prantl05097242018-04-30 16:49:04 +0000841 // rest of the bytes are the overflow from the addition.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 if (GetAddressByteSize() == 4)
843 load_addr &= 0xFFFFFFFF;
844
845 if (target.GetSectionLoadList().SetSectionLoadAddress(section_sp,
846 load_addr))
847 ++num_loaded_sections;
848 }
849 }
850 return num_loaded_sections > 0;
Steve Pucci9e02dac2014-02-06 19:02:19 +0000851 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852 }
853 return false;
854}
855
856ByteOrder ObjectFileELF::GetByteOrder() const {
857 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
858 return eByteOrderBig;
859 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
860 return eByteOrderLittle;
861 return eByteOrderInvalid;
862}
863
864uint32_t ObjectFileELF::GetAddressByteSize() const {
865 return m_data.GetAddressByteSize();
866}
867
868AddressClass ObjectFileELF::GetAddressClass(addr_t file_addr) {
869 Symtab *symtab = GetSymtab();
870 if (!symtab)
Tatyana Krasnukha04803b32018-06-26 13:06:54 +0000871 return AddressClass::eUnknown;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872
Adrian Prantl05097242018-04-30 16:49:04 +0000873 // The address class is determined based on the symtab. Ask it from the
874 // object file what contains the symtab information.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000875 ObjectFile *symtab_objfile = symtab->GetObjectFile();
876 if (symtab_objfile != nullptr && symtab_objfile != this)
877 return symtab_objfile->GetAddressClass(file_addr);
878
879 auto res = ObjectFile::GetAddressClass(file_addr);
Tatyana Krasnukha04803b32018-06-26 13:06:54 +0000880 if (res != AddressClass::eCode)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000881 return res;
882
883 auto ub = m_address_class_map.upper_bound(file_addr);
884 if (ub == m_address_class_map.begin()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000885 // No entry in the address class map before the address. Return default
886 // address class for an address in a code section.
Tatyana Krasnukha04803b32018-06-26 13:06:54 +0000887 return AddressClass::eCode;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000888 }
889
890 // Move iterator to the address class entry preceding address
891 --ub;
892
893 return ub->second;
894}
895
896size_t ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) {
897 return std::distance(m_section_headers.begin(), I) + 1u;
898}
899
900size_t ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const {
901 return std::distance(m_section_headers.begin(), I) + 1u;
902}
903
904bool ObjectFileELF::ParseHeader() {
905 lldb::offset_t offset = 0;
Pavel Labathdf1a0d12017-06-20 08:11:47 +0000906 return m_header.Parse(m_data, &offset);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000907}
908
Kate Stoneb9c1b512016-09-06 20:57:50 +0000909bool ObjectFileELF::GetUUID(lldb_private::UUID *uuid) {
910 // Need to parse the section list to get the UUIDs, so make sure that's been
911 // done.
912 if (!ParseSectionHeaders() && GetType() != ObjectFile::eTypeCoreFile)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000914
Pavel Labath77c397f2018-06-29 11:20:29 +0000915 using u32le = llvm::support::ulittle32_t;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000916 if (m_uuid.IsValid()) {
917 // We have the full build id uuid.
918 *uuid = m_uuid;
919 return true;
920 } else if (GetType() == ObjectFile::eTypeCoreFile) {
921 uint32_t core_notes_crc = 0;
922
923 if (!ParseProgramHeaders())
924 return false;
925
926 core_notes_crc = CalculateELFNotesSegmentsCRC32(m_program_headers, m_data);
927
928 if (core_notes_crc) {
Adrian Prantl05097242018-04-30 16:49:04 +0000929 // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it look
930 // different form .gnu_debuglink crc - followed by 4 bytes of note
Kate Stoneb9c1b512016-09-06 20:57:50 +0000931 // segments crc.
Pavel Labath77c397f2018-06-29 11:20:29 +0000932 u32le data[] = {u32le(g_core_uuid_magic), u32le(core_notes_crc)};
933 m_uuid = UUID::fromData(data, sizeof(data));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934 }
935 } else {
936 if (!m_gnu_debuglink_crc)
937 m_gnu_debuglink_crc =
938 calc_gnu_debuglink_crc32(m_data.GetDataStart(), m_data.GetByteSize());
939 if (m_gnu_debuglink_crc) {
940 // Use 4 bytes of crc from the .gnu_debuglink section.
Pavel Labath77c397f2018-06-29 11:20:29 +0000941 u32le data(m_gnu_debuglink_crc);
942 m_uuid = UUID::fromData(&data, sizeof(data));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000943 }
944 }
945
946 if (m_uuid.IsValid()) {
947 *uuid = m_uuid;
948 return true;
949 }
950
951 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000952}
953
Kate Stoneb9c1b512016-09-06 20:57:50 +0000954lldb_private::FileSpecList ObjectFileELF::GetDebugSymbolFilePaths() {
955 FileSpecList file_spec_list;
Michael Sartaina7499c92013-07-01 19:45:50 +0000956
Kate Stoneb9c1b512016-09-06 20:57:50 +0000957 if (!m_gnu_debuglink_file.empty()) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000958 FileSpec file_spec(m_gnu_debuglink_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000959 file_spec_list.Append(file_spec);
960 }
961 return file_spec_list;
Michael Sartaina7499c92013-07-01 19:45:50 +0000962}
963
Kate Stoneb9c1b512016-09-06 20:57:50 +0000964uint32_t ObjectFileELF::GetDependentModules(FileSpecList &files) {
965 size_t num_modules = ParseDependentModules();
966 uint32_t num_specs = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000967
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 for (unsigned i = 0; i < num_modules; ++i) {
969 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
970 num_specs++;
971 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000972
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973 return num_specs;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000974}
975
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976Address ObjectFileELF::GetImageInfoAddress(Target *target) {
977 if (!ParseDynamicSymbols())
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000978 return Address();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000979
980 SectionList *section_list = GetSectionList();
981 if (!section_list)
982 return Address();
983
984 // Find the SHT_DYNAMIC (.dynamic) section.
985 SectionSP dynsym_section_sp(
986 section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true));
987 if (!dynsym_section_sp)
988 return Address();
989 assert(dynsym_section_sp->GetObjectFile() == this);
990
991 user_id_t dynsym_id = dynsym_section_sp->GetID();
992 const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
993 if (!dynsym_hdr)
994 return Address();
995
996 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) {
997 ELFDynamic &symbol = m_dynamic_symbols[i];
998
999 if (symbol.d_tag == DT_DEBUG) {
Adrian Prantl05097242018-04-30 16:49:04 +00001000 // Compute the offset as the number of previous entries plus the size of
1001 // d_tag.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001002 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
1003 return Address(dynsym_section_sp, offset);
1004 }
1005 // MIPS executables uses DT_MIPS_RLD_MAP_REL to support PIE. DT_MIPS_RLD_MAP
1006 // exists in non-PIE.
1007 else if ((symbol.d_tag == DT_MIPS_RLD_MAP ||
1008 symbol.d_tag == DT_MIPS_RLD_MAP_REL) &&
1009 target) {
1010 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
1011 addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target);
1012 if (dyn_base == LLDB_INVALID_ADDRESS)
1013 return Address();
1014
Zachary Turner97206d52017-05-12 04:51:55 +00001015 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001016 if (symbol.d_tag == DT_MIPS_RLD_MAP) {
1017 // DT_MIPS_RLD_MAP tag stores an absolute address of the debug pointer.
1018 Address addr;
1019 if (target->ReadPointerFromMemory(dyn_base + offset, false, error,
1020 addr))
1021 return addr;
1022 }
1023 if (symbol.d_tag == DT_MIPS_RLD_MAP_REL) {
1024 // DT_MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,
1025 // relative to the address of the tag.
1026 uint64_t rel_offset;
1027 rel_offset = target->ReadUnsignedIntegerFromMemory(
1028 dyn_base + offset, false, GetAddressByteSize(), UINT64_MAX, error);
1029 if (error.Success() && rel_offset != UINT64_MAX) {
1030 Address addr;
1031 addr_t debug_ptr_address =
1032 dyn_base + (offset - GetAddressByteSize()) + rel_offset;
1033 addr.SetOffset(debug_ptr_address);
1034 return addr;
1035 }
1036 }
1037 }
1038 }
1039
1040 return Address();
Stephen Wilson2ab0a582011-01-15 00:08:44 +00001041}
1042
Kate Stoneb9c1b512016-09-06 20:57:50 +00001043lldb_private::Address ObjectFileELF::GetEntryPointAddress() {
1044 if (m_entry_point_address.IsValid())
Stephen Wilsond126c8c2011-03-08 04:12:15 +00001045 return m_entry_point_address;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001046
1047 if (!ParseHeader() || !IsExecutable())
1048 return m_entry_point_address;
1049
1050 SectionList *section_list = GetSectionList();
1051 addr_t offset = m_header.e_entry;
1052
1053 if (!section_list)
1054 m_entry_point_address.SetOffset(offset);
1055 else
1056 m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
1057 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +00001058}
1059
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001060//----------------------------------------------------------------------
1061// ParseDependentModules
1062//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001063size_t ObjectFileELF::ParseDependentModules() {
1064 if (m_filespec_ap.get())
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001065 return m_filespec_ap->GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001066
1067 m_filespec_ap.reset(new FileSpecList());
1068
1069 if (!ParseSectionHeaders())
1070 return 0;
1071
1072 SectionList *section_list = GetSectionList();
1073 if (!section_list)
1074 return 0;
1075
1076 // Find the SHT_DYNAMIC section.
1077 Section *dynsym =
1078 section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true)
1079 .get();
1080 if (!dynsym)
1081 return 0;
1082 assert(dynsym->GetObjectFile() == this);
1083
1084 const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex(dynsym->GetID());
1085 if (!header)
1086 return 0;
1087 // sh_link: section header index of string table used by entries in the
1088 // section.
1089 Section *dynstr = section_list->FindSectionByID(header->sh_link + 1).get();
1090 if (!dynstr)
1091 return 0;
1092
1093 DataExtractor dynsym_data;
1094 DataExtractor dynstr_data;
1095 if (ReadSectionData(dynsym, dynsym_data) &&
1096 ReadSectionData(dynstr, dynstr_data)) {
1097 ELFDynamic symbol;
1098 const lldb::offset_t section_size = dynsym_data.GetByteSize();
1099 lldb::offset_t offset = 0;
1100
1101 // The only type of entries we are concerned with are tagged DT_NEEDED,
1102 // yielding the name of a required library.
1103 while (offset < section_size) {
1104 if (!symbol.Parse(dynsym_data, &offset))
1105 break;
1106
1107 if (symbol.d_tag != DT_NEEDED)
1108 continue;
1109
1110 uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
1111 const char *lib_name = dynstr_data.PeekCStr(str_index);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001112 FileSpec file_spec(lib_name);
1113 FileSystem::Instance().Resolve(file_spec);
1114 m_filespec_ap->Append(file_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115 }
1116 }
1117
1118 return m_filespec_ap->GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001119}
1120
1121//----------------------------------------------------------------------
Todd Fiala4339f3a2014-03-25 19:29:09 +00001122// GetProgramHeaderInfo
1123//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001124size_t ObjectFileELF::GetProgramHeaderInfo(ProgramHeaderColl &program_headers,
Pavel Labathdf1a0d12017-06-20 08:11:47 +00001125 DataExtractor &object_data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 const ELFHeader &header) {
1127 // We have already parsed the program headers
1128 if (!program_headers.empty())
Todd Fiala4339f3a2014-03-25 19:29:09 +00001129 return program_headers.size();
1130
Kate Stoneb9c1b512016-09-06 20:57:50 +00001131 // If there are no program headers to read we are done.
1132 if (header.e_phnum == 0)
1133 return 0;
1134
1135 program_headers.resize(header.e_phnum);
1136 if (program_headers.size() != header.e_phnum)
1137 return 0;
1138
1139 const size_t ph_size = header.e_phnum * header.e_phentsize;
1140 const elf_off ph_offset = header.e_phoff;
1141 DataExtractor data;
Pavel Labathdf1a0d12017-06-20 08:11:47 +00001142 if (data.SetData(object_data, ph_offset, ph_size) != ph_size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001143 return 0;
1144
1145 uint32_t idx;
1146 lldb::offset_t offset;
1147 for (idx = 0, offset = 0; idx < header.e_phnum; ++idx) {
1148 if (program_headers[idx].Parse(data, &offset) == false)
1149 break;
1150 }
1151
1152 if (idx < program_headers.size())
1153 program_headers.resize(idx);
1154
1155 return program_headers.size();
Todd Fiala4339f3a2014-03-25 19:29:09 +00001156}
1157
1158//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001159// ParseProgramHeaders
1160//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001161size_t ObjectFileELF::ParseProgramHeaders() {
Pavel Labathdf1a0d12017-06-20 08:11:47 +00001162 return GetProgramHeaderInfo(m_program_headers, m_data, m_header);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163}
1164
Zachary Turner97206d52017-05-12 04:51:55 +00001165lldb_private::Status
Kate Stoneb9c1b512016-09-06 20:57:50 +00001166ObjectFileELF::RefineModuleDetailsFromNote(lldb_private::DataExtractor &data,
1167 lldb_private::ArchSpec &arch_spec,
1168 lldb_private::UUID &uuid) {
1169 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES));
Zachary Turner97206d52017-05-12 04:51:55 +00001170 Status error;
Todd Fialab91de782014-06-27 16:52:49 +00001171
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172 lldb::offset_t offset = 0;
Michael Sartainc836ae72013-05-23 20:57:03 +00001173
Kate Stoneb9c1b512016-09-06 20:57:50 +00001174 while (true) {
1175 // Parse the note header. If this fails, bail out.
1176 const lldb::offset_t note_offset = offset;
1177 ELFNote note = ELFNote();
1178 if (!note.Parse(data, &offset)) {
1179 // We're done.
1180 return error;
Michael Sartainc836ae72013-05-23 20:57:03 +00001181 }
Todd Fialab91de782014-06-27 16:52:49 +00001182
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183 if (log)
1184 log->Printf("ObjectFileELF::%s parsing note name='%s', type=%" PRIu32,
1185 __FUNCTION__, note.n_name.c_str(), note.n_type);
1186
1187 // Process FreeBSD ELF notes.
1188 if ((note.n_name == LLDB_NT_OWNER_FREEBSD) &&
1189 (note.n_type == LLDB_NT_FREEBSD_ABI_TAG) &&
1190 (note.n_descsz == LLDB_NT_FREEBSD_ABI_SIZE)) {
1191 // Pull out the min version info.
1192 uint32_t version_info;
1193 if (data.GetU32(&offset, &version_info, 1) == nullptr) {
1194 error.SetErrorString("failed to read FreeBSD ABI note payload");
1195 return error;
1196 }
1197
1198 // Convert the version info into a major/minor number.
1199 const uint32_t version_major = version_info / 100000;
1200 const uint32_t version_minor = (version_info / 1000) % 100;
1201
1202 char os_name[32];
1203 snprintf(os_name, sizeof(os_name), "freebsd%" PRIu32 ".%" PRIu32,
1204 version_major, version_minor);
1205
1206 // Set the elf OS version to FreeBSD. Also clear the vendor.
1207 arch_spec.GetTriple().setOSName(os_name);
1208 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
1209
1210 if (log)
1211 log->Printf("ObjectFileELF::%s detected FreeBSD %" PRIu32 ".%" PRIu32
1212 ".%" PRIu32,
1213 __FUNCTION__, version_major, version_minor,
1214 static_cast<uint32_t>(version_info % 1000));
1215 }
1216 // Process GNU ELF notes.
1217 else if (note.n_name == LLDB_NT_OWNER_GNU) {
1218 switch (note.n_type) {
1219 case LLDB_NT_GNU_ABI_TAG:
1220 if (note.n_descsz == LLDB_NT_GNU_ABI_SIZE) {
1221 // Pull out the min OS version supporting the ABI.
1222 uint32_t version_info[4];
1223 if (data.GetU32(&offset, &version_info[0], note.n_descsz / 4) ==
1224 nullptr) {
1225 error.SetErrorString("failed to read GNU ABI note payload");
1226 return error;
1227 }
1228
1229 // Set the OS per the OS field.
1230 switch (version_info[0]) {
1231 case LLDB_NT_GNU_ABI_OS_LINUX:
1232 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1233 arch_spec.GetTriple().setVendor(
1234 llvm::Triple::VendorType::UnknownVendor);
1235 if (log)
1236 log->Printf(
1237 "ObjectFileELF::%s detected Linux, min version %" PRIu32
1238 ".%" PRIu32 ".%" PRIu32,
1239 __FUNCTION__, version_info[1], version_info[2],
1240 version_info[3]);
1241 // FIXME we have the minimal version number, we could be propagating
1242 // that. version_info[1] = OS Major, version_info[2] = OS Minor,
1243 // version_info[3] = Revision.
1244 break;
1245 case LLDB_NT_GNU_ABI_OS_HURD:
1246 arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS);
1247 arch_spec.GetTriple().setVendor(
1248 llvm::Triple::VendorType::UnknownVendor);
1249 if (log)
1250 log->Printf("ObjectFileELF::%s detected Hurd (unsupported), min "
1251 "version %" PRIu32 ".%" PRIu32 ".%" PRIu32,
1252 __FUNCTION__, version_info[1], version_info[2],
1253 version_info[3]);
1254 break;
1255 case LLDB_NT_GNU_ABI_OS_SOLARIS:
1256 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Solaris);
1257 arch_spec.GetTriple().setVendor(
1258 llvm::Triple::VendorType::UnknownVendor);
1259 if (log)
1260 log->Printf(
1261 "ObjectFileELF::%s detected Solaris, min version %" PRIu32
1262 ".%" PRIu32 ".%" PRIu32,
1263 __FUNCTION__, version_info[1], version_info[2],
1264 version_info[3]);
1265 break;
1266 default:
1267 if (log)
1268 log->Printf(
1269 "ObjectFileELF::%s unrecognized OS in note, id %" PRIu32
1270 ", min version %" PRIu32 ".%" PRIu32 ".%" PRIu32,
1271 __FUNCTION__, version_info[0], version_info[1],
1272 version_info[2], version_info[3]);
1273 break;
1274 }
1275 }
1276 break;
1277
1278 case LLDB_NT_GNU_BUILD_ID_TAG:
1279 // Only bother processing this if we don't already have the uuid set.
1280 if (!uuid.IsValid()) {
1281 // 16 bytes is UUID|MD5, 20 bytes is SHA1. Other linkers may produce a
Pavel Labath77c397f2018-06-29 11:20:29 +00001282 // build-id of a different length. Accept it as long as it's at least
1283 // 4 bytes as it will be better than our own crc32.
1284 if (note.n_descsz >= 4) {
1285 if (const uint8_t *buf = data.PeekData(offset, note.n_descsz)) {
1286 // Save the build id as the UUID for the module.
1287 uuid = UUID::fromData(buf, note.n_descsz);
1288 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001289 error.SetErrorString("failed to read GNU_BUILD_ID note payload");
1290 return error;
1291 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001292 }
1293 }
1294 break;
1295 }
Nitesh Jain706c5202017-03-31 11:06:25 +00001296 if (arch_spec.IsMIPS() &&
1297 arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS)
1298 // The note.n_name == LLDB_NT_OWNER_GNU is valid for Linux platform
1299 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001300 }
1301 // Process NetBSD ELF notes.
1302 else if ((note.n_name == LLDB_NT_OWNER_NETBSD) &&
1303 (note.n_type == LLDB_NT_NETBSD_ABI_TAG) &&
1304 (note.n_descsz == LLDB_NT_NETBSD_ABI_SIZE)) {
1305 // Pull out the min version info.
1306 uint32_t version_info;
1307 if (data.GetU32(&offset, &version_info, 1) == nullptr) {
1308 error.SetErrorString("failed to read NetBSD ABI note payload");
1309 return error;
1310 }
1311
1312 // Set the elf OS version to NetBSD. Also clear the vendor.
1313 arch_spec.GetTriple().setOS(llvm::Triple::OSType::NetBSD);
1314 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
1315
1316 if (log)
1317 log->Printf(
1318 "ObjectFileELF::%s detected NetBSD, min version constant %" PRIu32,
1319 __FUNCTION__, version_info);
1320 }
Kamil Rytarowski12801f12017-03-26 15:34:57 +00001321 // Process OpenBSD ELF notes.
1322 else if (note.n_name == LLDB_NT_OWNER_OPENBSD) {
1323 // Set the elf OS version to OpenBSD. Also clear the vendor.
1324 arch_spec.GetTriple().setOS(llvm::Triple::OSType::OpenBSD);
1325 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);
1326 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001327 // Process CSR kalimba notes
1328 else if ((note.n_type == LLDB_NT_GNU_ABI_TAG) &&
1329 (note.n_name == LLDB_NT_OWNER_CSR)) {
1330 arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS);
1331 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::CSR);
1332
1333 // TODO At some point the description string could be processed.
Adrian Prantl05097242018-04-30 16:49:04 +00001334 // It could provide a steer towards the kalimba variant which this ELF
1335 // targets.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001336 if (note.n_descsz) {
1337 const char *cstr =
1338 data.GetCStr(&offset, llvm::alignTo(note.n_descsz, 4));
1339 (void)cstr;
1340 }
1341 } else if (note.n_name == LLDB_NT_OWNER_ANDROID) {
1342 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1343 arch_spec.GetTriple().setEnvironment(
1344 llvm::Triple::EnvironmentType::Android);
1345 } else if (note.n_name == LLDB_NT_OWNER_LINUX) {
1346 // This is sometimes found in core files and usually contains extended
1347 // register info
1348 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1349 } else if (note.n_name == LLDB_NT_OWNER_CORE) {
Adrian Prantl05097242018-04-30 16:49:04 +00001350 // Parse the NT_FILE to look for stuff in paths to shared libraries As
1351 // the contents look like this in a 64 bit ELF core file: count =
1352 // 0x000000000000000a (10) page_size = 0x0000000000001000 (4096) Index
1353 // start end file_ofs path =====
1354 // ------------------ ------------------ ------------------
1355 // ------------------------------------- [ 0] 0x0000000000400000
1356 // 0x0000000000401000 0x0000000000000000 /tmp/a.out [ 1]
1357 // 0x0000000000600000 0x0000000000601000 0x0000000000000000 /tmp/a.out [
1358 // 2] 0x0000000000601000 0x0000000000602000 0x0000000000000001 /tmp/a.out
Kate Stoneb9c1b512016-09-06 20:57:50 +00001359 // [ 3] 0x00007fa79c9ed000 0x00007fa79cba8000 0x0000000000000000
Adrian Prantl05097242018-04-30 16:49:04 +00001360 // /lib/x86_64-linux-gnu/libc-2.19.so [ 4] 0x00007fa79cba8000
1361 // 0x00007fa79cda7000 0x00000000000001bb /lib/x86_64-linux-
1362 // gnu/libc-2.19.so [ 5] 0x00007fa79cda7000 0x00007fa79cdab000
1363 // 0x00000000000001ba /lib/x86_64-linux-gnu/libc-2.19.so [ 6]
1364 // 0x00007fa79cdab000 0x00007fa79cdad000 0x00000000000001be /lib/x86_64
1365 // -linux-gnu/libc-2.19.so [ 7] 0x00007fa79cdb2000 0x00007fa79cdd5000
1366 // 0x0000000000000000 /lib/x86_64-linux-gnu/ld-2.19.so [ 8]
1367 // 0x00007fa79cfd4000 0x00007fa79cfd5000 0x0000000000000022 /lib/x86_64
1368 // -linux-gnu/ld-2.19.so [ 9] 0x00007fa79cfd5000 0x00007fa79cfd6000
1369 // 0x0000000000000023 /lib/x86_64-linux-gnu/ld-2.19.so In the 32 bit ELFs
1370 // the count, page_size, start, end, file_ofs are uint32_t For reference:
1371 // see readelf source code (in binutils).
Kate Stoneb9c1b512016-09-06 20:57:50 +00001372 if (note.n_type == NT_FILE) {
1373 uint64_t count = data.GetAddress(&offset);
1374 const char *cstr;
1375 data.GetAddress(&offset); // Skip page size
1376 offset += count * 3 *
1377 data.GetAddressByteSize(); // Skip all start/end/file_ofs
1378 for (size_t i = 0; i < count; ++i) {
1379 cstr = data.GetCStr(&offset);
1380 if (cstr == nullptr) {
1381 error.SetErrorStringWithFormat("ObjectFileELF::%s trying to read "
1382 "at an offset after the end "
1383 "(GetCStr returned nullptr)",
1384 __FUNCTION__);
1385 return error;
1386 }
1387 llvm::StringRef path(cstr);
Richard Chamberlaina0c82e12016-10-13 12:11:00 +00001388 if (path.contains("/lib/x86_64-linux-gnu") || path.contains("/lib/i386-linux-gnu")) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001389 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
1390 break;
1391 }
1392 }
Nitesh Jain706c5202017-03-31 11:06:25 +00001393 if (arch_spec.IsMIPS() &&
1394 arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS)
Adrian Prantl05097242018-04-30 16:49:04 +00001395 // In case of MIPSR6, the LLDB_NT_OWNER_GNU note is missing for some
1396 // cases (e.g. compile with -nostdlib) Hence set OS to Linux
Leonard Mosescu9ba51572018-08-07 18:00:30 +00001397 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001398 }
1399 }
1400
Adrian Prantl05097242018-04-30 16:49:04 +00001401 // Calculate the offset of the next note just in case "offset" has been
1402 // used to poke at the contents of the note data
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403 offset = note_offset + note.GetByteSize();
1404 }
1405
1406 return error;
Michael Sartainc836ae72013-05-23 20:57:03 +00001407}
Michael Sartaina7499c92013-07-01 19:45:50 +00001408
Kate Stoneb9c1b512016-09-06 20:57:50 +00001409void ObjectFileELF::ParseARMAttributes(DataExtractor &data, uint64_t length,
1410 ArchSpec &arch_spec) {
1411 lldb::offset_t Offset = 0;
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001412
Kate Stoneb9c1b512016-09-06 20:57:50 +00001413 uint8_t FormatVersion = data.GetU8(&Offset);
1414 if (FormatVersion != llvm::ARMBuildAttrs::Format_Version)
1415 return;
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001416
Kate Stoneb9c1b512016-09-06 20:57:50 +00001417 Offset = Offset + sizeof(uint32_t); // Section Length
1418 llvm::StringRef VendorName = data.GetCStr(&Offset);
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001419
Kate Stoneb9c1b512016-09-06 20:57:50 +00001420 if (VendorName != "aeabi")
1421 return;
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001422
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423 if (arch_spec.GetTriple().getEnvironment() ==
1424 llvm::Triple::UnknownEnvironment)
1425 arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI);
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001426
Kate Stoneb9c1b512016-09-06 20:57:50 +00001427 while (Offset < length) {
1428 uint8_t Tag = data.GetU8(&Offset);
1429 uint32_t Size = data.GetU32(&Offset);
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001430
Kate Stoneb9c1b512016-09-06 20:57:50 +00001431 if (Tag != llvm::ARMBuildAttrs::File || Size == 0)
1432 continue;
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001433
Kate Stoneb9c1b512016-09-06 20:57:50 +00001434 while (Offset < length) {
1435 uint64_t Tag = data.GetULEB128(&Offset);
1436 switch (Tag) {
1437 default:
1438 if (Tag < 32)
1439 data.GetULEB128(&Offset);
1440 else if (Tag % 2 == 0)
1441 data.GetULEB128(&Offset);
1442 else
1443 data.GetCStr(&Offset);
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001444
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445 break;
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001446
Kate Stoneb9c1b512016-09-06 20:57:50 +00001447 case llvm::ARMBuildAttrs::CPU_raw_name:
1448 case llvm::ARMBuildAttrs::CPU_name:
1449 data.GetCStr(&Offset);
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001450
Kate Stoneb9c1b512016-09-06 20:57:50 +00001451 break;
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001452
Kate Stoneb9c1b512016-09-06 20:57:50 +00001453 case llvm::ARMBuildAttrs::ABI_VFP_args: {
1454 uint64_t VFPArgs = data.GetULEB128(&Offset);
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001455
Kate Stoneb9c1b512016-09-06 20:57:50 +00001456 if (VFPArgs == llvm::ARMBuildAttrs::BaseAAPCS) {
1457 if (arch_spec.GetTriple().getEnvironment() ==
1458 llvm::Triple::UnknownEnvironment ||
1459 arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABIHF)
1460 arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI);
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001461
Kate Stoneb9c1b512016-09-06 20:57:50 +00001462 arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float);
1463 } else if (VFPArgs == llvm::ARMBuildAttrs::HardFPAAPCS) {
1464 if (arch_spec.GetTriple().getEnvironment() ==
1465 llvm::Triple::UnknownEnvironment ||
1466 arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABI)
1467 arch_spec.GetTriple().setEnvironment(llvm::Triple::EABIHF);
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001468
Kate Stoneb9c1b512016-09-06 20:57:50 +00001469 arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float);
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001470 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001471
1472 break;
1473 }
1474 }
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001475 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001476 }
Saleem Abdulrasoold2d15042016-04-23 16:00:15 +00001477}
Todd Fialab91de782014-06-27 16:52:49 +00001478
Michael Sartaina7499c92013-07-01 19:45:50 +00001479//----------------------------------------------------------------------
1480// GetSectionHeaderInfo
1481//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001482size_t ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl &section_headers,
Pavel Labathdf1a0d12017-06-20 08:11:47 +00001483 DataExtractor &object_data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001484 const elf::ELFHeader &header,
1485 lldb_private::UUID &uuid,
1486 std::string &gnu_debuglink_file,
1487 uint32_t &gnu_debuglink_crc,
1488 ArchSpec &arch_spec) {
1489 // Don't reparse the section headers if we already did that.
1490 if (!section_headers.empty())
1491 return section_headers.size();
Todd Fiala6477ea82014-07-11 15:13:33 +00001492
Kate Stoneb9c1b512016-09-06 20:57:50 +00001493 // Only initialize the arch_spec to okay defaults if they're not already set.
1494 // We'll refine this with note data as we parse the notes.
1495 if (arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) {
1496 llvm::Triple::OSType ostype;
1497 llvm::Triple::OSType spec_ostype;
1498 const uint32_t sub_type = subTypeFromElfHeader(header);
1499 arch_spec.SetArchitecture(eArchTypeELF, header.e_machine, sub_type,
1500 header.e_ident[EI_OSABI]);
Leonard Mosescu9ba51572018-08-07 18:00:30 +00001501
Adrian Prantl05097242018-04-30 16:49:04 +00001502 // Validate if it is ok to remove GetOsFromOSABI. Note, that now the OS is
1503 // determined based on EI_OSABI flag and the info extracted from ELF notes
1504 // (see RefineModuleDetailsFromNote). However in some cases that still
1505 // might be not enough: for example a shared library might not have any
1506 // notes at all and have EI_OSABI flag set to System V, as result the OS
1507 // will be set to UnknownOS.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001508 GetOsFromOSABI(header.e_ident[EI_OSABI], ostype);
1509 spec_ostype = arch_spec.GetTriple().getOS();
1510 assert(spec_ostype == ostype);
Hafiz Abid Qadeerb1554312017-01-20 10:24:03 +00001511 UNUSED_IF_ASSERT_DISABLED(spec_ostype);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001512 }
1513
1514 if (arch_spec.GetMachine() == llvm::Triple::mips ||
1515 arch_spec.GetMachine() == llvm::Triple::mipsel ||
1516 arch_spec.GetMachine() == llvm::Triple::mips64 ||
1517 arch_spec.GetMachine() == llvm::Triple::mips64el) {
1518 switch (header.e_flags & llvm::ELF::EF_MIPS_ARCH_ASE) {
1519 case llvm::ELF::EF_MIPS_MICROMIPS:
1520 arch_spec.SetFlags(ArchSpec::eMIPSAse_micromips);
1521 break;
1522 case llvm::ELF::EF_MIPS_ARCH_ASE_M16:
1523 arch_spec.SetFlags(ArchSpec::eMIPSAse_mips16);
1524 break;
1525 case llvm::ELF::EF_MIPS_ARCH_ASE_MDMX:
1526 arch_spec.SetFlags(ArchSpec::eMIPSAse_mdmx);
1527 break;
1528 default:
1529 break;
Todd Fialab91de782014-06-27 16:52:49 +00001530 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001531 }
Todd Fialab91de782014-06-27 16:52:49 +00001532
Kate Stoneb9c1b512016-09-06 20:57:50 +00001533 if (arch_spec.GetMachine() == llvm::Triple::arm ||
1534 arch_spec.GetMachine() == llvm::Triple::thumb) {
1535 if (header.e_flags & llvm::ELF::EF_ARM_SOFT_FLOAT)
1536 arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float);
1537 else if (header.e_flags & llvm::ELF::EF_ARM_VFP_FLOAT)
1538 arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float);
1539 }
Jaydeep Patil501a7812015-07-16 03:51:55 +00001540
Kate Stoneb9c1b512016-09-06 20:57:50 +00001541 // If there are no section headers we are done.
1542 if (header.e_shnum == 0)
Michael Sartaina7499c92013-07-01 19:45:50 +00001543 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001544
1545 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES));
1546
1547 section_headers.resize(header.e_shnum);
1548 if (section_headers.size() != header.e_shnum)
1549 return 0;
1550
1551 const size_t sh_size = header.e_shnum * header.e_shentsize;
1552 const elf_off sh_offset = header.e_shoff;
1553 DataExtractor sh_data;
Pavel Labathdf1a0d12017-06-20 08:11:47 +00001554 if (sh_data.SetData(object_data, sh_offset, sh_size) != sh_size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001555 return 0;
1556
1557 uint32_t idx;
1558 lldb::offset_t offset;
1559 for (idx = 0, offset = 0; idx < header.e_shnum; ++idx) {
1560 if (section_headers[idx].Parse(sh_data, &offset) == false)
1561 break;
1562 }
1563 if (idx < section_headers.size())
1564 section_headers.resize(idx);
1565
1566 const unsigned strtab_idx = header.e_shstrndx;
1567 if (strtab_idx && strtab_idx < section_headers.size()) {
1568 const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx];
1569 const size_t byte_size = sheader.sh_size;
1570 const Elf64_Off offset = sheader.sh_offset;
1571 lldb_private::DataExtractor shstr_data;
1572
Pavel Labathdf1a0d12017-06-20 08:11:47 +00001573 if (shstr_data.SetData(object_data, offset, byte_size) == byte_size) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001574 for (SectionHeaderCollIter I = section_headers.begin();
1575 I != section_headers.end(); ++I) {
1576 static ConstString g_sect_name_gnu_debuglink(".gnu_debuglink");
1577 const ELFSectionHeaderInfo &sheader = *I;
1578 const uint64_t section_size =
1579 sheader.sh_type == SHT_NOBITS ? 0 : sheader.sh_size;
1580 ConstString name(shstr_data.PeekCStr(I->sh_name));
1581
1582 I->section_name = name;
1583
1584 if (arch_spec.IsMIPS()) {
1585 uint32_t arch_flags = arch_spec.GetFlags();
1586 DataExtractor data;
1587 if (sheader.sh_type == SHT_MIPS_ABIFLAGS) {
1588
Pavel Labathdf1a0d12017-06-20 08:11:47 +00001589 if (section_size && (data.SetData(object_data, sheader.sh_offset,
1590 section_size) == section_size)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591 // MIPS ASE Mask is at offset 12 in MIPS.abiflags section
1592 lldb::offset_t offset = 12; // MIPS ABI Flags Version: 0
1593 arch_flags |= data.GetU32(&offset);
1594
1595 // The floating point ABI is at offset 7
1596 offset = 7;
1597 switch (data.GetU8(&offset)) {
1598 case llvm::Mips::Val_GNU_MIPS_ABI_FP_ANY:
1599 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_ANY;
1600 break;
1601 case llvm::Mips::Val_GNU_MIPS_ABI_FP_DOUBLE:
1602 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_DOUBLE;
1603 break;
1604 case llvm::Mips::Val_GNU_MIPS_ABI_FP_SINGLE:
1605 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SINGLE;
1606 break;
1607 case llvm::Mips::Val_GNU_MIPS_ABI_FP_SOFT:
1608 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SOFT;
1609 break;
1610 case llvm::Mips::Val_GNU_MIPS_ABI_FP_OLD_64:
1611 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_OLD_64;
1612 break;
1613 case llvm::Mips::Val_GNU_MIPS_ABI_FP_XX:
1614 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_XX;
1615 break;
1616 case llvm::Mips::Val_GNU_MIPS_ABI_FP_64:
1617 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64;
1618 break;
1619 case llvm::Mips::Val_GNU_MIPS_ABI_FP_64A:
1620 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64A;
1621 break;
1622 }
1623 }
1624 }
1625 // Settings appropriate ArchSpec ABI Flags
1626 switch (header.e_flags & llvm::ELF::EF_MIPS_ABI) {
1627 case llvm::ELF::EF_MIPS_ABI_O32:
1628 arch_flags |= lldb_private::ArchSpec::eMIPSABI_O32;
1629 break;
1630 case EF_MIPS_ABI_O64:
1631 arch_flags |= lldb_private::ArchSpec::eMIPSABI_O64;
1632 break;
1633 case EF_MIPS_ABI_EABI32:
1634 arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI32;
1635 break;
1636 case EF_MIPS_ABI_EABI64:
1637 arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI64;
1638 break;
1639 default:
1640 // ABI Mask doesn't cover N32 and N64 ABI.
1641 if (header.e_ident[EI_CLASS] == llvm::ELF::ELFCLASS64)
1642 arch_flags |= lldb_private::ArchSpec::eMIPSABI_N64;
Mehdi Aminid16a6e32017-06-23 18:20:13 +00001643 else if (header.e_flags & llvm::ELF::EF_MIPS_ABI2)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001644 arch_flags |= lldb_private::ArchSpec::eMIPSABI_N32;
1645 break;
1646 }
1647 arch_spec.SetFlags(arch_flags);
1648 }
1649
1650 if (arch_spec.GetMachine() == llvm::Triple::arm ||
1651 arch_spec.GetMachine() == llvm::Triple::thumb) {
1652 DataExtractor data;
1653
1654 if (sheader.sh_type == SHT_ARM_ATTRIBUTES && section_size != 0 &&
Pavel Labathdf1a0d12017-06-20 08:11:47 +00001655 data.SetData(object_data, sheader.sh_offset, section_size) == section_size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001656 ParseARMAttributes(data, section_size, arch_spec);
1657 }
1658
1659 if (name == g_sect_name_gnu_debuglink) {
1660 DataExtractor data;
Pavel Labathdf1a0d12017-06-20 08:11:47 +00001661 if (section_size && (data.SetData(object_data, sheader.sh_offset,
1662 section_size) == section_size)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001663 lldb::offset_t gnu_debuglink_offset = 0;
1664 gnu_debuglink_file = data.GetCStr(&gnu_debuglink_offset);
1665 gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4);
1666 data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1);
1667 }
1668 }
1669
1670 // Process ELF note section entries.
1671 bool is_note_header = (sheader.sh_type == SHT_NOTE);
1672
1673 // The section header ".note.android.ident" is stored as a
1674 // PROGBITS type header but it is actually a note header.
1675 static ConstString g_sect_name_android_ident(".note.android.ident");
1676 if (!is_note_header && name == g_sect_name_android_ident)
1677 is_note_header = true;
1678
1679 if (is_note_header) {
1680 // Allow notes to refine module info.
1681 DataExtractor data;
Pavel Labathdf1a0d12017-06-20 08:11:47 +00001682 if (section_size && (data.SetData(object_data, sheader.sh_offset,
1683 section_size) == section_size)) {
Zachary Turner97206d52017-05-12 04:51:55 +00001684 Status error = RefineModuleDetailsFromNote(data, arch_spec, uuid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001685 if (error.Fail()) {
1686 if (log)
1687 log->Printf("ObjectFileELF::%s ELF note processing failed: %s",
1688 __FUNCTION__, error.AsCString());
1689 }
1690 }
1691 }
1692 }
1693
1694 // Make any unknown triple components to be unspecified unknowns.
1695 if (arch_spec.GetTriple().getVendor() == llvm::Triple::UnknownVendor)
1696 arch_spec.GetTriple().setVendorName(llvm::StringRef());
1697 if (arch_spec.GetTriple().getOS() == llvm::Triple::UnknownOS)
1698 arch_spec.GetTriple().setOSName(llvm::StringRef());
1699
1700 return section_headers.size();
1701 }
1702 }
1703
1704 section_headers.clear();
1705 return 0;
Michael Sartaina7499c92013-07-01 19:45:50 +00001706}
1707
Kate Stoneb9c1b512016-09-06 20:57:50 +00001708size_t ObjectFileELF::GetProgramHeaderCount() { return ParseProgramHeaders(); }
Ashok Thirumurthi4822d922013-07-11 20:39:00 +00001709
1710const elf::ELFProgramHeader *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001711ObjectFileELF::GetProgramHeaderByIndex(lldb::user_id_t id) {
1712 if (!id || !ParseProgramHeaders())
Ashok Thirumurthi4822d922013-07-11 20:39:00 +00001713 return NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001714
1715 if (--id < m_program_headers.size())
1716 return &m_program_headers[id];
1717
1718 return NULL;
Ashok Thirumurthi4822d922013-07-11 20:39:00 +00001719}
1720
Kate Stoneb9c1b512016-09-06 20:57:50 +00001721DataExtractor ObjectFileELF::GetSegmentDataByIndex(lldb::user_id_t id) {
1722 const elf::ELFProgramHeader *segment_header = GetProgramHeaderByIndex(id);
1723 if (segment_header == NULL)
1724 return DataExtractor();
1725 return DataExtractor(m_data, segment_header->p_offset,
1726 segment_header->p_filesz);
Ashok Thirumurthi4822d922013-07-11 20:39:00 +00001727}
1728
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001729llvm::StringRef
Kate Stoneb9c1b512016-09-06 20:57:50 +00001730ObjectFileELF::StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const {
1731 size_t pos = symbol_name.find('@');
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001732 return symbol_name.substr(0, pos);
Pavel Labathc6ae7ea2015-03-04 10:25:22 +00001733}
1734
Michael Sartaina7499c92013-07-01 19:45:50 +00001735//----------------------------------------------------------------------
1736// ParseSectionHeaders
1737//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001738size_t ObjectFileELF::ParseSectionHeaders() {
Pavel Labathdf1a0d12017-06-20 08:11:47 +00001739 return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid,
1740 m_gnu_debuglink_file, m_gnu_debuglink_crc,
1741 m_arch_spec);
Michael Sartaina7499c92013-07-01 19:45:50 +00001742}
1743
Michael Sartaina7499c92013-07-01 19:45:50 +00001744const ObjectFileELF::ELFSectionHeaderInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001745ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) {
1746 if (!id || !ParseSectionHeaders())
Michael Sartaina7499c92013-07-01 19:45:50 +00001747 return NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001748
1749 if (--id < m_section_headers.size())
1750 return &m_section_headers[id];
1751
1752 return NULL;
Michael Sartaina7499c92013-07-01 19:45:50 +00001753}
1754
Kate Stoneb9c1b512016-09-06 20:57:50 +00001755lldb::user_id_t ObjectFileELF::GetSectionIndexByName(const char *name) {
1756 if (!name || !name[0] || !ParseSectionHeaders())
Tamas Berghammer85fadd92015-05-08 09:40:05 +00001757 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001758 for (size_t i = 1; i < m_section_headers.size(); ++i)
1759 if (m_section_headers[i].section_name == ConstString(name))
1760 return i;
1761 return 0;
Tamas Berghammer85fadd92015-05-08 09:40:05 +00001762}
1763
Kate Stoneb9c1b512016-09-06 20:57:50 +00001764void ObjectFileELF::CreateSections(SectionList &unified_section_list) {
1765 if (!m_sections_ap.get() && ParseSectionHeaders()) {
1766 m_sections_ap.reset(new SectionList());
Andrew MacPherson17220c12014-03-05 10:12:43 +00001767
Ed Masted13f6912017-10-02 14:35:07 +00001768 // Object files frequently have 0 for every section address, meaning we
1769 // need to compute synthetic addresses in order for "file addresses" from
1770 // different sections to not overlap
1771 bool synthaddrs = (CalculateType() == ObjectFile::Type::eTypeObjectFile);
1772 uint64_t nextaddr = 0;
1773
Andrew MacPherson17220c12014-03-05 10:12:43 +00001774 for (SectionHeaderCollIter I = m_section_headers.begin();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001775 I != m_section_headers.end(); ++I) {
1776 const ELFSectionHeaderInfo &header = *I;
1777
Davide Italiano1e6a01f2018-05-12 01:25:48 +00001778 ConstString &name = I->section_name;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001779 const uint64_t file_size =
1780 header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
1781 const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0;
1782
Davide Italiano1e6a01f2018-05-12 01:25:48 +00001783 static ConstString g_sect_name_text(".text");
1784 static ConstString g_sect_name_data(".data");
1785 static ConstString g_sect_name_bss(".bss");
1786 static ConstString g_sect_name_tdata(".tdata");
1787 static ConstString g_sect_name_tbss(".tbss");
1788 static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev");
1789 static ConstString g_sect_name_dwarf_debug_addr(".debug_addr");
1790 static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges");
1791 static ConstString g_sect_name_dwarf_debug_cu_index(".debug_cu_index");
1792 static ConstString g_sect_name_dwarf_debug_frame(".debug_frame");
1793 static ConstString g_sect_name_dwarf_debug_info(".debug_info");
1794 static ConstString g_sect_name_dwarf_debug_line(".debug_line");
George Rimarc6c7bfc2018-09-13 17:06:47 +00001795 static ConstString g_sect_name_dwarf_debug_line_str(".debug_line_str");
Davide Italiano1e6a01f2018-05-12 01:25:48 +00001796 static ConstString g_sect_name_dwarf_debug_loc(".debug_loc");
George Rimare4dee262018-10-23 09:46:15 +00001797 static ConstString g_sect_name_dwarf_debug_loclists(".debug_loclists");
Davide Italiano1e6a01f2018-05-12 01:25:48 +00001798 static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo");
1799 static ConstString g_sect_name_dwarf_debug_macro(".debug_macro");
Pavel Labatha041d842018-06-01 12:06:45 +00001800 static ConstString g_sect_name_dwarf_debug_names(".debug_names");
Davide Italiano1e6a01f2018-05-12 01:25:48 +00001801 static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames");
1802 static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes");
1803 static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges");
George Rimar6e357122018-10-10 08:11:15 +00001804 static ConstString g_sect_name_dwarf_debug_rnglists(".debug_rnglists");
Davide Italiano1e6a01f2018-05-12 01:25:48 +00001805 static ConstString g_sect_name_dwarf_debug_str(".debug_str");
1806 static ConstString g_sect_name_dwarf_debug_str_offsets(
1807 ".debug_str_offsets");
1808 static ConstString g_sect_name_dwarf_debug_abbrev_dwo(
1809 ".debug_abbrev.dwo");
1810 static ConstString g_sect_name_dwarf_debug_info_dwo(".debug_info.dwo");
1811 static ConstString g_sect_name_dwarf_debug_line_dwo(".debug_line.dwo");
George Rimarc6c7bfc2018-09-13 17:06:47 +00001812 static ConstString g_sect_name_dwarf_debug_line_str_dwo(".debug_line_str.dwo");
Davide Italiano1e6a01f2018-05-12 01:25:48 +00001813 static ConstString g_sect_name_dwarf_debug_macro_dwo(".debug_macro.dwo");
1814 static ConstString g_sect_name_dwarf_debug_loc_dwo(".debug_loc.dwo");
George Rimare4dee262018-10-23 09:46:15 +00001815 static ConstString g_sect_name_dwarf_debug_loclists_dwo(".debug_loclists.dwo");
Davide Italiano1e6a01f2018-05-12 01:25:48 +00001816 static ConstString g_sect_name_dwarf_debug_str_dwo(".debug_str.dwo");
1817 static ConstString g_sect_name_dwarf_debug_str_offsets_dwo(
1818 ".debug_str_offsets.dwo");
1819 static ConstString g_sect_name_dwarf_debug_types(".debug_types");
1820 static ConstString g_sect_name_eh_frame(".eh_frame");
1821 static ConstString g_sect_name_arm_exidx(".ARM.exidx");
1822 static ConstString g_sect_name_arm_extab(".ARM.extab");
1823 static ConstString g_sect_name_go_symtab(".gosymtab");
1824 static ConstString g_sect_name_dwarf_gnu_debugaltlink(".gnu_debugaltlink");
1825
1826 SectionType sect_type = eSectionTypeOther;
1827
1828 bool is_thread_specific = false;
1829
1830 if (name == g_sect_name_text)
1831 sect_type = eSectionTypeCode;
1832 else if (name == g_sect_name_data)
1833 sect_type = eSectionTypeData;
1834 else if (name == g_sect_name_bss)
1835 sect_type = eSectionTypeZeroFill;
1836 else if (name == g_sect_name_tdata) {
1837 sect_type = eSectionTypeData;
1838 is_thread_specific = true;
1839 } else if (name == g_sect_name_tbss) {
1840 sect_type = eSectionTypeZeroFill;
1841 is_thread_specific = true;
1842 }
1843 // .debug_abbrev – Abbreviations used in the .debug_info section
1844 // .debug_aranges – Lookup table for mapping addresses to compilation
1845 // units .debug_frame – Call frame information .debug_info – The core
1846 // DWARF information section .debug_line – Line number information
1847 // .debug_loc – Location lists used in DW_AT_location attributes
1848 // .debug_macinfo – Macro information .debug_pubnames – Lookup table
1849 // for mapping object and function names to compilation units
1850 // .debug_pubtypes – Lookup table for mapping type names to compilation
1851 // units .debug_ranges – Address ranges used in DW_AT_ranges attributes
1852 // .debug_str – String table used in .debug_info MISSING?
1853 // .gnu_debugdata - "mini debuginfo / MiniDebugInfo" section,
1854 // http://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html MISSING?
1855 // .debug-index - http://src.chromium.org/viewvc/chrome/trunk/src/build
1856 // /gdb-add-index?pathrev=144644 MISSING? .debug_types - Type
1857 // descriptions from DWARF 4? See
1858 // http://gcc.gnu.org/wiki/DwarfSeparateTypeInfo
1859 else if (name == g_sect_name_dwarf_debug_abbrev)
1860 sect_type = eSectionTypeDWARFDebugAbbrev;
1861 else if (name == g_sect_name_dwarf_debug_addr)
1862 sect_type = eSectionTypeDWARFDebugAddr;
1863 else if (name == g_sect_name_dwarf_debug_aranges)
1864 sect_type = eSectionTypeDWARFDebugAranges;
1865 else if (name == g_sect_name_dwarf_debug_cu_index)
1866 sect_type = eSectionTypeDWARFDebugCuIndex;
1867 else if (name == g_sect_name_dwarf_debug_frame)
1868 sect_type = eSectionTypeDWARFDebugFrame;
1869 else if (name == g_sect_name_dwarf_debug_info)
1870 sect_type = eSectionTypeDWARFDebugInfo;
1871 else if (name == g_sect_name_dwarf_debug_line)
1872 sect_type = eSectionTypeDWARFDebugLine;
George Rimarc6c7bfc2018-09-13 17:06:47 +00001873 else if (name == g_sect_name_dwarf_debug_line_str)
1874 sect_type = eSectionTypeDWARFDebugLineStr;
Davide Italiano1e6a01f2018-05-12 01:25:48 +00001875 else if (name == g_sect_name_dwarf_debug_loc)
1876 sect_type = eSectionTypeDWARFDebugLoc;
George Rimare4dee262018-10-23 09:46:15 +00001877 else if (name == g_sect_name_dwarf_debug_loclists)
1878 sect_type = eSectionTypeDWARFDebugLocLists;
Davide Italiano1e6a01f2018-05-12 01:25:48 +00001879 else if (name == g_sect_name_dwarf_debug_macinfo)
1880 sect_type = eSectionTypeDWARFDebugMacInfo;
1881 else if (name == g_sect_name_dwarf_debug_macro)
1882 sect_type = eSectionTypeDWARFDebugMacro;
Pavel Labatha041d842018-06-01 12:06:45 +00001883 else if (name == g_sect_name_dwarf_debug_names)
1884 sect_type = eSectionTypeDWARFDebugNames;
Davide Italiano1e6a01f2018-05-12 01:25:48 +00001885 else if (name == g_sect_name_dwarf_debug_pubnames)
1886 sect_type = eSectionTypeDWARFDebugPubNames;
1887 else if (name == g_sect_name_dwarf_debug_pubtypes)
1888 sect_type = eSectionTypeDWARFDebugPubTypes;
1889 else if (name == g_sect_name_dwarf_debug_ranges)
1890 sect_type = eSectionTypeDWARFDebugRanges;
George Rimar6e357122018-10-10 08:11:15 +00001891 else if (name == g_sect_name_dwarf_debug_rnglists)
1892 sect_type = eSectionTypeDWARFDebugRngLists;
Davide Italiano1e6a01f2018-05-12 01:25:48 +00001893 else if (name == g_sect_name_dwarf_debug_str)
1894 sect_type = eSectionTypeDWARFDebugStr;
1895 else if (name == g_sect_name_dwarf_debug_types)
1896 sect_type = eSectionTypeDWARFDebugTypes;
1897 else if (name == g_sect_name_dwarf_debug_str_offsets)
1898 sect_type = eSectionTypeDWARFDebugStrOffsets;
1899 else if (name == g_sect_name_dwarf_debug_abbrev_dwo)
1900 sect_type = eSectionTypeDWARFDebugAbbrev;
1901 else if (name == g_sect_name_dwarf_debug_info_dwo)
1902 sect_type = eSectionTypeDWARFDebugInfo;
1903 else if (name == g_sect_name_dwarf_debug_line_dwo)
1904 sect_type = eSectionTypeDWARFDebugLine;
George Rimarc6c7bfc2018-09-13 17:06:47 +00001905 else if (name == g_sect_name_dwarf_debug_line_str_dwo)
1906 sect_type = eSectionTypeDWARFDebugLineStr;
Davide Italiano1e6a01f2018-05-12 01:25:48 +00001907 else if (name == g_sect_name_dwarf_debug_macro_dwo)
1908 sect_type = eSectionTypeDWARFDebugMacro;
1909 else if (name == g_sect_name_dwarf_debug_loc_dwo)
1910 sect_type = eSectionTypeDWARFDebugLoc;
George Rimare4dee262018-10-23 09:46:15 +00001911 else if (name == g_sect_name_dwarf_debug_loclists_dwo)
1912 sect_type = eSectionTypeDWARFDebugLocLists;
Davide Italiano1e6a01f2018-05-12 01:25:48 +00001913 else if (name == g_sect_name_dwarf_debug_str_dwo)
1914 sect_type = eSectionTypeDWARFDebugStr;
1915 else if (name == g_sect_name_dwarf_debug_str_offsets_dwo)
1916 sect_type = eSectionTypeDWARFDebugStrOffsets;
1917 else if (name == g_sect_name_eh_frame)
1918 sect_type = eSectionTypeEHFrame;
1919 else if (name == g_sect_name_arm_exidx)
1920 sect_type = eSectionTypeARMexidx;
1921 else if (name == g_sect_name_arm_extab)
1922 sect_type = eSectionTypeARMextab;
1923 else if (name == g_sect_name_go_symtab)
1924 sect_type = eSectionTypeGoSymtab;
1925 else if (name == g_sect_name_dwarf_gnu_debugaltlink)
1926 sect_type = eSectionTypeDWARFGNUDebugAltLink;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001927
1928 const uint32_t permissions =
Ilia K4f730dc2016-09-12 05:25:33 +00001929 ((header.sh_flags & SHF_ALLOC) ? ePermissionsReadable : 0u) |
1930 ((header.sh_flags & SHF_WRITE) ? ePermissionsWritable : 0u) |
1931 ((header.sh_flags & SHF_EXECINSTR) ? ePermissionsExecutable : 0u);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001932 switch (header.sh_type) {
1933 case SHT_SYMTAB:
1934 assert(sect_type == eSectionTypeOther);
1935 sect_type = eSectionTypeELFSymbolTable;
1936 break;
1937 case SHT_DYNSYM:
1938 assert(sect_type == eSectionTypeOther);
1939 sect_type = eSectionTypeELFDynamicSymbols;
1940 break;
1941 case SHT_RELA:
1942 case SHT_REL:
1943 assert(sect_type == eSectionTypeOther);
1944 sect_type = eSectionTypeELFRelocationEntries;
1945 break;
1946 case SHT_DYNAMIC:
1947 assert(sect_type == eSectionTypeOther);
1948 sect_type = eSectionTypeELFDynamicLinkInfo;
1949 break;
1950 }
1951
1952 if (eSectionTypeOther == sect_type) {
1953 // the kalimba toolchain assumes that ELF section names are free-form.
Adrian Prantl05097242018-04-30 16:49:04 +00001954 // It does support linkscripts which (can) give rise to various
1955 // arbitrarily named sections being "Code" or "Data".
Kate Stoneb9c1b512016-09-06 20:57:50 +00001956 sect_type = kalimbaSectionType(m_header, header);
1957 }
1958
Pavel Labathedb01272018-04-30 13:23:47 +00001959 // In common case ELF code section can have arbitrary name (for example,
1960 // we can specify it using section attribute for particular function) so
1961 // assume that section is a code section if it has SHF_EXECINSTR flag set
1962 // and has SHT_PROGBITS type.
1963 if (eSectionTypeOther == sect_type &&
1964 llvm::ELF::SHT_PROGBITS == header.sh_type &&
1965 (header.sh_flags & SHF_EXECINSTR)) {
1966 sect_type = eSectionTypeCode;
1967 }
1968
Kate Stoneb9c1b512016-09-06 20:57:50 +00001969 const uint32_t target_bytes_size =
1970 (eSectionTypeData == sect_type || eSectionTypeZeroFill == sect_type)
1971 ? m_arch_spec.GetDataByteSize()
1972 : eSectionTypeCode == sect_type ? m_arch_spec.GetCodeByteSize()
1973 : 1;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001974 elf::elf_xword log2align =
1975 (header.sh_addralign == 0) ? 0 : llvm::Log2_64(header.sh_addralign);
Ed Masted13f6912017-10-02 14:35:07 +00001976
1977 uint64_t addr = header.sh_addr;
1978
1979 if ((header.sh_flags & SHF_ALLOC) && synthaddrs) {
1980 nextaddr =
1981 (nextaddr + header.sh_addralign - 1) & ~(header.sh_addralign - 1);
1982 addr = nextaddr;
1983 nextaddr += vm_size;
1984 }
1985
Kate Stoneb9c1b512016-09-06 20:57:50 +00001986 SectionSP section_sp(new Section(
1987 GetModule(), // Module to which this section belongs.
1988 this, // ObjectFile to which this section belongs and should read
1989 // section data from.
1990 SectionIndex(I), // Section ID.
Davide Italiano1e6a01f2018-05-12 01:25:48 +00001991 name, // Section name.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001992 sect_type, // Section type.
Ed Masted13f6912017-10-02 14:35:07 +00001993 addr, // VM address.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001994 vm_size, // VM size in bytes of this section.
1995 header.sh_offset, // Offset of this section in the file.
1996 file_size, // Size of the section as found in the file.
1997 log2align, // Alignment of the section
1998 header.sh_flags, // Flags for this section.
1999 target_bytes_size)); // Number of host bytes per target byte
2000
2001 section_sp->SetPermissions(permissions);
2002 if (is_thread_specific)
2003 section_sp->SetIsThreadSpecific(is_thread_specific);
2004 m_sections_ap->AddSection(section_sp);
Andrew MacPherson17220c12014-03-05 10:12:43 +00002005 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002006 }
2007
Pavel Labath3ef4eeb2018-03-09 12:30:09 +00002008 // For eTypeDebugInfo files, the Symbol Vendor will take care of updating the
2009 // unified section list.
2010 if (GetType() != eTypeDebugInfo)
2011 unified_section_list = *m_sections_ap;
Greg Clayton3046e662013-07-10 01:23:25 +00002012}
2013
Kate Stoneb9c1b512016-09-06 20:57:50 +00002014// Find the arm/aarch64 mapping symbol character in the given symbol name.
Adrian Prantl05097242018-04-30 16:49:04 +00002015// Mapping symbols have the form of "$<char>[.<any>]*". Additionally we
2016// recognize cases when the mapping symbol prefixed by an arbitrary string
2017// because if a symbol prefix added to each symbol in the object file with
Kate Stoneb9c1b512016-09-06 20:57:50 +00002018// objcopy then the mapping symbols are also prefixed.
2019static char FindArmAarch64MappingSymbol(const char *symbol_name) {
2020 if (!symbol_name)
2021 return '\0';
2022
2023 const char *dollar_pos = ::strchr(symbol_name, '$');
2024 if (!dollar_pos || dollar_pos[1] == '\0')
2025 return '\0';
2026
2027 if (dollar_pos[2] == '\0' || dollar_pos[2] == '.')
2028 return dollar_pos[1];
2029 return '\0';
2030}
2031
2032#define STO_MIPS_ISA (3 << 6)
2033#define STO_MICROMIPS (2 << 6)
2034#define IS_MICROMIPS(ST_OTHER) (((ST_OTHER)&STO_MIPS_ISA) == STO_MICROMIPS)
2035
2036// private
2037unsigned ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
2038 SectionList *section_list,
2039 const size_t num_symbols,
2040 const DataExtractor &symtab_data,
2041 const DataExtractor &strtab_data) {
2042 ELFSymbol symbol;
2043 lldb::offset_t offset = 0;
2044
2045 static ConstString text_section_name(".text");
2046 static ConstString init_section_name(".init");
2047 static ConstString fini_section_name(".fini");
2048 static ConstString ctors_section_name(".ctors");
2049 static ConstString dtors_section_name(".dtors");
2050
2051 static ConstString data_section_name(".data");
2052 static ConstString rodata_section_name(".rodata");
2053 static ConstString rodata1_section_name(".rodata1");
2054 static ConstString data2_section_name(".data1");
2055 static ConstString bss_section_name(".bss");
2056 static ConstString opd_section_name(".opd"); // For ppc64
2057
2058 // On Android the oatdata and the oatexec symbols in the oat and odex files
Adrian Prantl05097242018-04-30 16:49:04 +00002059 // covers the full .text section what causes issues with displaying unusable
2060 // symbol name to the user and very slow unwinding speed because the
2061 // instruction emulation based unwind plans try to emulate all instructions
2062 // in these symbols. Don't add these symbols to the symbol list as they have
2063 // no use for the debugger and they are causing a lot of trouble. Filtering
2064 // can't be restricted to Android because this special object file don't
2065 // contain the note section specifying the environment to Android but the
2066 // custom extension and file name makes it highly unlikely that this will
2067 // collide with anything else.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002068 ConstString file_extension = m_file.GetFileNameExtension();
Jonas Devliegheread8d48f2018-06-13 16:23:21 +00002069 bool skip_oatdata_oatexec = file_extension == ConstString(".oat") ||
2070 file_extension == ConstString(".odex");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002071
2072 ArchSpec arch;
2073 GetArchitecture(arch);
2074 ModuleSP module_sp(GetModule());
2075 SectionList *module_section_list =
2076 module_sp ? module_sp->GetSectionList() : nullptr;
2077
2078 // Local cache to avoid doing a FindSectionByName for each symbol. The "const
Adrian Prantl05097242018-04-30 16:49:04 +00002079 // char*" key must came from a ConstString object so they can be compared by
2080 // pointer
Kate Stoneb9c1b512016-09-06 20:57:50 +00002081 std::unordered_map<const char *, lldb::SectionSP> section_name_to_section;
2082
2083 unsigned i;
2084 for (i = 0; i < num_symbols; ++i) {
2085 if (symbol.Parse(symtab_data, &offset) == false)
2086 break;
2087
2088 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
2089 if (!symbol_name)
2090 symbol_name = "";
2091
2092 // No need to add non-section symbols that have no names
2093 if (symbol.getType() != STT_SECTION &&
2094 (symbol_name == nullptr || symbol_name[0] == '\0'))
2095 continue;
2096
2097 // Skipping oatdata and oatexec sections if it is requested. See details
Adrian Prantl05097242018-04-30 16:49:04 +00002098 // above the definition of skip_oatdata_oatexec for the reasons.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002099 if (skip_oatdata_oatexec && (::strcmp(symbol_name, "oatdata") == 0 ||
2100 ::strcmp(symbol_name, "oatexec") == 0))
2101 continue;
2102
2103 SectionSP symbol_section_sp;
2104 SymbolType symbol_type = eSymbolTypeInvalid;
2105 Elf64_Half section_idx = symbol.st_shndx;
2106
2107 switch (section_idx) {
2108 case SHN_ABS:
2109 symbol_type = eSymbolTypeAbsolute;
2110 break;
2111 case SHN_UNDEF:
2112 symbol_type = eSymbolTypeUndefined;
2113 break;
2114 default:
2115 symbol_section_sp = section_list->GetSectionAtIndex(section_idx);
2116 break;
2117 }
2118
2119 // If a symbol is undefined do not process it further even if it has a STT
2120 // type
2121 if (symbol_type != eSymbolTypeUndefined) {
2122 switch (symbol.getType()) {
2123 default:
2124 case STT_NOTYPE:
2125 // The symbol's type is not specified.
2126 break;
2127
2128 case STT_OBJECT:
Adrian Prantl05097242018-04-30 16:49:04 +00002129 // The symbol is associated with a data object, such as a variable, an
2130 // array, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002131 symbol_type = eSymbolTypeData;
2132 break;
2133
2134 case STT_FUNC:
2135 // The symbol is associated with a function or other executable code.
2136 symbol_type = eSymbolTypeCode;
2137 break;
2138
2139 case STT_SECTION:
2140 // The symbol is associated with a section. Symbol table entries of
Adrian Prantl05097242018-04-30 16:49:04 +00002141 // this type exist primarily for relocation and normally have STB_LOCAL
2142 // binding.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002143 break;
2144
2145 case STT_FILE:
Adrian Prantl05097242018-04-30 16:49:04 +00002146 // Conventionally, the symbol's name gives the name of the source file
2147 // associated with the object file. A file symbol has STB_LOCAL
Kate Stoneb9c1b512016-09-06 20:57:50 +00002148 // binding, its section index is SHN_ABS, and it precedes the other
2149 // STB_LOCAL symbols for the file, if it is present.
2150 symbol_type = eSymbolTypeSourceFile;
2151 break;
2152
2153 case STT_GNU_IFUNC:
2154 // The symbol is associated with an indirect function. The actual
2155 // function will be resolved if it is referenced.
2156 symbol_type = eSymbolTypeResolver;
2157 break;
2158 }
2159 }
2160
2161 if (symbol_type == eSymbolTypeInvalid && symbol.getType() != STT_SECTION) {
2162 if (symbol_section_sp) {
2163 const ConstString &sect_name = symbol_section_sp->GetName();
2164 if (sect_name == text_section_name || sect_name == init_section_name ||
2165 sect_name == fini_section_name || sect_name == ctors_section_name ||
2166 sect_name == dtors_section_name) {
2167 symbol_type = eSymbolTypeCode;
2168 } else if (sect_name == data_section_name ||
2169 sect_name == data2_section_name ||
2170 sect_name == rodata_section_name ||
2171 sect_name == rodata1_section_name ||
2172 sect_name == bss_section_name) {
2173 symbol_type = eSymbolTypeData;
2174 }
2175 }
2176 }
2177
2178 int64_t symbol_value_offset = 0;
2179 uint32_t additional_flags = 0;
2180
2181 if (arch.IsValid()) {
2182 if (arch.GetMachine() == llvm::Triple::arm) {
2183 if (symbol.getBinding() == STB_LOCAL) {
2184 char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);
2185 if (symbol_type == eSymbolTypeCode) {
2186 switch (mapping_symbol) {
2187 case 'a':
2188 // $a[.<any>]* - marks an ARM instruction sequence
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00002189 m_address_class_map[symbol.st_value] = AddressClass::eCode;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002190 break;
2191 case 'b':
2192 case 't':
2193 // $b[.<any>]* - marks a THUMB BL instruction sequence
2194 // $t[.<any>]* - marks a THUMB instruction sequence
2195 m_address_class_map[symbol.st_value] =
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00002196 AddressClass::eCodeAlternateISA;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002197 break;
2198 case 'd':
2199 // $d[.<any>]* - marks a data item sequence (e.g. lit pool)
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00002200 m_address_class_map[symbol.st_value] = AddressClass::eData;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002201 break;
2202 }
2203 }
2204 if (mapping_symbol)
2205 continue;
2206 }
2207 } else if (arch.GetMachine() == llvm::Triple::aarch64) {
2208 if (symbol.getBinding() == STB_LOCAL) {
2209 char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);
2210 if (symbol_type == eSymbolTypeCode) {
2211 switch (mapping_symbol) {
2212 case 'x':
2213 // $x[.<any>]* - marks an A64 instruction sequence
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00002214 m_address_class_map[symbol.st_value] = AddressClass::eCode;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002215 break;
2216 case 'd':
2217 // $d[.<any>]* - marks a data item sequence (e.g. lit pool)
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00002218 m_address_class_map[symbol.st_value] = AddressClass::eData;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002219 break;
2220 }
2221 }
2222 if (mapping_symbol)
2223 continue;
2224 }
2225 }
2226
2227 if (arch.GetMachine() == llvm::Triple::arm) {
2228 if (symbol_type == eSymbolTypeCode) {
2229 if (symbol.st_value & 1) {
Adrian Prantl05097242018-04-30 16:49:04 +00002230 // Subtracting 1 from the address effectively unsets the low order
2231 // bit, which results in the address actually pointing to the
2232 // beginning of the symbol. This delta will be used below in
2233 // conjunction with symbol.st_value to produce the final
2234 // symbol_value that we store in the symtab.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002235 symbol_value_offset = -1;
2236 m_address_class_map[symbol.st_value ^ 1] =
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00002237 AddressClass::eCodeAlternateISA;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002238 } else {
2239 // This address is ARM
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00002240 m_address_class_map[symbol.st_value] = AddressClass::eCode;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002241 }
2242 }
2243 }
2244
2245 /*
2246 * MIPS:
2247 * The bit #0 of an address is used for ISA mode (1 for microMIPS, 0 for
2248 * MIPS).
2249 * This allows processor to switch between microMIPS and MIPS without any
2250 * need
2251 * for special mode-control register. However, apart from .debug_line,
2252 * none of
2253 * the ELF/DWARF sections set the ISA bit (for symbol or section). Use
2254 * st_other
2255 * flag to check whether the symbol is microMIPS and then set the address
2256 * class
2257 * accordingly.
2258 */
2259 const llvm::Triple::ArchType llvm_arch = arch.GetMachine();
2260 if (llvm_arch == llvm::Triple::mips ||
2261 llvm_arch == llvm::Triple::mipsel ||
2262 llvm_arch == llvm::Triple::mips64 ||
2263 llvm_arch == llvm::Triple::mips64el) {
2264 if (IS_MICROMIPS(symbol.st_other))
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00002265 m_address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002266 else if ((symbol.st_value & 1) && (symbol_type == eSymbolTypeCode)) {
2267 symbol.st_value = symbol.st_value & (~1ull);
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00002268 m_address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002269 } else {
2270 if (symbol_type == eSymbolTypeCode)
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00002271 m_address_class_map[symbol.st_value] = AddressClass::eCode;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002272 else if (symbol_type == eSymbolTypeData)
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00002273 m_address_class_map[symbol.st_value] = AddressClass::eData;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002274 else
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00002275 m_address_class_map[symbol.st_value] = AddressClass::eUnknown;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002276 }
2277 }
2278 }
2279
2280 // symbol_value_offset may contain 0 for ARM symbols or -1 for THUMB
Adrian Prantl05097242018-04-30 16:49:04 +00002281 // symbols. See above for more details.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002282 uint64_t symbol_value = symbol.st_value + symbol_value_offset;
2283
2284 if (symbol_section_sp == nullptr && section_idx == SHN_ABS &&
2285 symbol.st_size != 0) {
2286 // We don't have a section for a symbol with non-zero size. Create a new
Adrian Prantl05097242018-04-30 16:49:04 +00002287 // section for it so the address range covered by the symbol is also
2288 // covered by the module (represented through the section list). It is
2289 // needed so module lookup for the addresses covered by this symbol will
2290 // be successfull. This case happens for absolute symbols.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002291 ConstString fake_section_name(std::string(".absolute.") + symbol_name);
2292 symbol_section_sp =
2293 std::make_shared<Section>(module_sp, this, SHN_ABS, fake_section_name,
2294 eSectionTypeAbsoluteAddress, symbol_value,
2295 symbol.st_size, 0, 0, 0, SHF_ALLOC);
2296
2297 module_section_list->AddSection(symbol_section_sp);
2298 section_list->AddSection(symbol_section_sp);
2299 }
2300
2301 if (symbol_section_sp &&
2302 CalculateType() != ObjectFile::Type::eTypeObjectFile)
2303 symbol_value -= symbol_section_sp->GetFileAddress();
2304
2305 if (symbol_section_sp && module_section_list &&
2306 module_section_list != section_list) {
2307 const ConstString &sect_name = symbol_section_sp->GetName();
2308 auto section_it = section_name_to_section.find(sect_name.GetCString());
2309 if (section_it == section_name_to_section.end())
2310 section_it =
2311 section_name_to_section
2312 .emplace(sect_name.GetCString(),
2313 module_section_list->FindSectionByName(sect_name))
2314 .first;
Pavel Labathefddda3d2017-05-02 12:40:31 +00002315 if (section_it->second)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002316 symbol_section_sp = section_it->second;
2317 }
2318
2319 bool is_global = symbol.getBinding() == STB_GLOBAL;
2320 uint32_t flags = symbol.st_other << 8 | symbol.st_info | additional_flags;
2321 bool is_mangled = (symbol_name[0] == '_' && symbol_name[1] == 'Z');
2322
2323 llvm::StringRef symbol_ref(symbol_name);
2324
2325 // Symbol names may contain @VERSION suffixes. Find those and strip them
2326 // temporarily.
2327 size_t version_pos = symbol_ref.find('@');
2328 bool has_suffix = version_pos != llvm::StringRef::npos;
2329 llvm::StringRef symbol_bare = symbol_ref.substr(0, version_pos);
2330 Mangled mangled(ConstString(symbol_bare), is_mangled);
2331
2332 // Now append the suffix back to mangled and unmangled names. Only do it if
Adrian Prantl05097242018-04-30 16:49:04 +00002333 // the demangling was successful (string is not empty).
Kate Stoneb9c1b512016-09-06 20:57:50 +00002334 if (has_suffix) {
2335 llvm::StringRef suffix = symbol_ref.substr(version_pos);
2336
2337 llvm::StringRef mangled_name = mangled.GetMangledName().GetStringRef();
2338 if (!mangled_name.empty())
2339 mangled.SetMangledName(ConstString((mangled_name + suffix).str()));
2340
2341 ConstString demangled =
2342 mangled.GetDemangledName(lldb::eLanguageTypeUnknown);
2343 llvm::StringRef demangled_name = demangled.GetStringRef();
2344 if (!demangled_name.empty())
2345 mangled.SetDemangledName(ConstString((demangled_name + suffix).str()));
2346 }
2347
2348 // In ELF all symbol should have a valid size but it is not true for some
Adrian Prantl05097242018-04-30 16:49:04 +00002349 // function symbols coming from hand written assembly. As none of the
2350 // function symbol should have 0 size we try to calculate the size for
2351 // these symbols in the symtab with saying that their original size is not
2352 // valid.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002353 bool symbol_size_valid =
2354 symbol.st_size != 0 || symbol.getType() != STT_FUNC;
2355
2356 Symbol dc_symbol(
2357 i + start_id, // ID is the original symbol table index.
2358 mangled,
2359 symbol_type, // Type of this symbol
2360 is_global, // Is this globally visible?
2361 false, // Is this symbol debug info?
2362 false, // Is this symbol a trampoline?
2363 false, // Is this symbol artificial?
2364 AddressRange(symbol_section_sp, // Section in which this symbol is
2365 // defined or null.
2366 symbol_value, // Offset in section or symbol value.
2367 symbol.st_size), // Size in bytes of this symbol.
2368 symbol_size_valid, // Symbol size is valid
2369 has_suffix, // Contains linker annotations?
2370 flags); // Symbol flags.
2371 symtab->AddSymbol(dc_symbol);
2372 }
2373 return i;
2374}
2375
2376unsigned ObjectFileELF::ParseSymbolTable(Symtab *symbol_table,
2377 user_id_t start_id,
2378 lldb_private::Section *symtab) {
2379 if (symtab->GetObjectFile() != this) {
2380 // If the symbol table section is owned by a different object file, have it
Adrian Prantl05097242018-04-30 16:49:04 +00002381 // do the parsing.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002382 ObjectFileELF *obj_file_elf =
2383 static_cast<ObjectFileELF *>(symtab->GetObjectFile());
2384 return obj_file_elf->ParseSymbolTable(symbol_table, start_id, symtab);
2385 }
2386
2387 // Get section list for this object file.
2388 SectionList *section_list = m_sections_ap.get();
2389 if (!section_list)
2390 return 0;
2391
2392 user_id_t symtab_id = symtab->GetID();
2393 const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
2394 assert(symtab_hdr->sh_type == SHT_SYMTAB ||
2395 symtab_hdr->sh_type == SHT_DYNSYM);
2396
Adrian Prantl05097242018-04-30 16:49:04 +00002397 // sh_link: section header index of associated string table. Section ID's are
2398 // ones based.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002399 user_id_t strtab_id = symtab_hdr->sh_link + 1;
2400 Section *strtab = section_list->FindSectionByID(strtab_id).get();
2401
2402 if (symtab && strtab) {
2403 assert(symtab->GetObjectFile() == this);
2404 assert(strtab->GetObjectFile() == this);
2405
2406 DataExtractor symtab_data;
2407 DataExtractor strtab_data;
2408 if (ReadSectionData(symtab, symtab_data) &&
2409 ReadSectionData(strtab, strtab_data)) {
2410 size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize;
2411
2412 return ParseSymbols(symbol_table, start_id, section_list, num_symbols,
2413 symtab_data, strtab_data);
2414 }
2415 }
2416
2417 return 0;
2418}
2419
2420size_t ObjectFileELF::ParseDynamicSymbols() {
2421 if (m_dynamic_symbols.size())
2422 return m_dynamic_symbols.size();
2423
2424 SectionList *section_list = GetSectionList();
2425 if (!section_list)
2426 return 0;
2427
2428 // Find the SHT_DYNAMIC section.
2429 Section *dynsym =
2430 section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true)
2431 .get();
2432 if (!dynsym)
2433 return 0;
2434 assert(dynsym->GetObjectFile() == this);
2435
2436 ELFDynamic symbol;
2437 DataExtractor dynsym_data;
2438 if (ReadSectionData(dynsym, dynsym_data)) {
2439 const lldb::offset_t section_size = dynsym_data.GetByteSize();
2440 lldb::offset_t cursor = 0;
2441
2442 while (cursor < section_size) {
2443 if (!symbol.Parse(dynsym_data, &cursor))
2444 break;
2445
2446 m_dynamic_symbols.push_back(symbol);
2447 }
2448 }
2449
2450 return m_dynamic_symbols.size();
2451}
2452
2453const ELFDynamic *ObjectFileELF::FindDynamicSymbol(unsigned tag) {
2454 if (!ParseDynamicSymbols())
2455 return NULL;
2456
2457 DynamicSymbolCollIter I = m_dynamic_symbols.begin();
2458 DynamicSymbolCollIter E = m_dynamic_symbols.end();
2459 for (; I != E; ++I) {
2460 ELFDynamic *symbol = &*I;
2461
2462 if (symbol->d_tag == tag)
2463 return symbol;
2464 }
2465
2466 return NULL;
2467}
2468
2469unsigned ObjectFileELF::PLTRelocationType() {
2470 // DT_PLTREL
2471 // This member specifies the type of relocation entry to which the
2472 // procedure linkage table refers. The d_val member holds DT_REL or
2473 // DT_RELA, as appropriate. All relocations in a procedure linkage table
2474 // must use the same relocation.
2475 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
2476
2477 if (symbol)
2478 return symbol->d_val;
2479
2480 return 0;
2481}
2482
Adrian Prantl05097242018-04-30 16:49:04 +00002483// Returns the size of the normal plt entries and the offset of the first
2484// normal plt entry. The 0th entry in the plt table is usually a resolution
2485// entry which have different size in some architectures then the rest of the
2486// plt entries.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002487static std::pair<uint64_t, uint64_t>
2488GetPltEntrySizeAndOffset(const ELFSectionHeader *rel_hdr,
2489 const ELFSectionHeader *plt_hdr) {
2490 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2491
Adrian Prantl05097242018-04-30 16:49:04 +00002492 // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are
2493 // 16 bytes. So round the entsize up by the alignment if addralign is set.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002494 elf_xword plt_entsize =
2495 plt_hdr->sh_addralign
2496 ? llvm::alignTo(plt_hdr->sh_entsize, plt_hdr->sh_addralign)
2497 : plt_hdr->sh_entsize;
2498
2499 // Some linkers e.g ld for arm, fill plt_hdr->sh_entsize field incorrectly.
2500 // PLT entries relocation code in general requires multiple instruction and
2501 // should be greater than 4 bytes in most cases. Try to guess correct size
2502 // just in case.
2503 if (plt_entsize <= 4) {
2504 // The linker haven't set the plt_hdr->sh_entsize field. Try to guess the
Adrian Prantl05097242018-04-30 16:49:04 +00002505 // size of the plt entries based on the number of entries and the size of
2506 // the plt section with the assumption that the size of the 0th entry is at
2507 // least as big as the size of the normal entries and it isn't much bigger
2508 // then that.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002509 if (plt_hdr->sh_addralign)
2510 plt_entsize = plt_hdr->sh_size / plt_hdr->sh_addralign /
2511 (num_relocations + 1) * plt_hdr->sh_addralign;
2512 else
2513 plt_entsize = plt_hdr->sh_size / (num_relocations + 1);
2514 }
2515
2516 elf_xword plt_offset = plt_hdr->sh_size - num_relocations * plt_entsize;
2517
2518 return std::make_pair(plt_entsize, plt_offset);
2519}
2520
2521static unsigned ParsePLTRelocations(
2522 Symtab *symbol_table, user_id_t start_id, unsigned rel_type,
2523 const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,
2524 const ELFSectionHeader *plt_hdr, const ELFSectionHeader *sym_hdr,
2525 const lldb::SectionSP &plt_section_sp, DataExtractor &rel_data,
2526 DataExtractor &symtab_data, DataExtractor &strtab_data) {
2527 ELFRelocation rel(rel_type);
2528 ELFSymbol symbol;
2529 lldb::offset_t offset = 0;
2530
2531 uint64_t plt_offset, plt_entsize;
2532 std::tie(plt_entsize, plt_offset) =
2533 GetPltEntrySizeAndOffset(rel_hdr, plt_hdr);
2534 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2535
2536 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
2537 reloc_info_fn reloc_type;
2538 reloc_info_fn reloc_symbol;
2539
2540 if (hdr->Is32Bit()) {
2541 reloc_type = ELFRelocation::RelocType32;
2542 reloc_symbol = ELFRelocation::RelocSymbol32;
2543 } else {
2544 reloc_type = ELFRelocation::RelocType64;
2545 reloc_symbol = ELFRelocation::RelocSymbol64;
2546 }
2547
2548 unsigned slot_type = hdr->GetRelocationJumpSlotType();
2549 unsigned i;
2550 for (i = 0; i < num_relocations; ++i) {
2551 if (rel.Parse(rel_data, &offset) == false)
2552 break;
2553
2554 if (reloc_type(rel) != slot_type)
2555 continue;
2556
2557 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
2558 if (!symbol.Parse(symtab_data, &symbol_offset))
2559 break;
2560
2561 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
2562 bool is_mangled =
2563 symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
2564 uint64_t plt_index = plt_offset + i * plt_entsize;
2565
2566 Symbol jump_symbol(
2567 i + start_id, // Symbol table index
2568 symbol_name, // symbol name.
2569 is_mangled, // is the symbol name mangled?
2570 eSymbolTypeTrampoline, // Type of this symbol
2571 false, // Is this globally visible?
2572 false, // Is this symbol debug info?
2573 true, // Is this symbol a trampoline?
2574 true, // Is this symbol artificial?
2575 plt_section_sp, // Section in which this symbol is defined or null.
2576 plt_index, // Offset in section or symbol value.
2577 plt_entsize, // Size in bytes of this symbol.
2578 true, // Size is valid
2579 false, // Contains linker annotations?
2580 0); // Symbol flags.
2581
2582 symbol_table->AddSymbol(jump_symbol);
2583 }
2584
2585 return i;
2586}
2587
2588unsigned
2589ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, user_id_t start_id,
2590 const ELFSectionHeaderInfo *rel_hdr,
2591 user_id_t rel_id) {
2592 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
2593
2594 // The link field points to the associated symbol table.
2595 user_id_t symtab_id = rel_hdr->sh_link;
2596
2597 // If the link field doesn't point to the appropriate symbol name table then
2598 // try to find it by name as some compiler don't fill in the link fields.
2599 if (!symtab_id)
2600 symtab_id = GetSectionIndexByName(".dynsym");
2601
2602 // Get PLT section. We cannot use rel_hdr->sh_info, since current linkers
2603 // point that to the .got.plt or .got section instead of .plt.
2604 user_id_t plt_id = GetSectionIndexByName(".plt");
2605
2606 if (!symtab_id || !plt_id)
2607 return 0;
2608
2609 // Section ID's are ones based;
2610 symtab_id++;
2611 plt_id++;
2612
2613 const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id);
2614 if (!plt_hdr)
2615 return 0;
2616
2617 const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id);
2618 if (!sym_hdr)
2619 return 0;
2620
2621 SectionList *section_list = m_sections_ap.get();
2622 if (!section_list)
2623 return 0;
2624
2625 Section *rel_section = section_list->FindSectionByID(rel_id).get();
2626 if (!rel_section)
2627 return 0;
2628
2629 SectionSP plt_section_sp(section_list->FindSectionByID(plt_id));
2630 if (!plt_section_sp)
2631 return 0;
2632
2633 Section *symtab = section_list->FindSectionByID(symtab_id).get();
2634 if (!symtab)
2635 return 0;
2636
2637 // sh_link points to associated string table.
2638 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get();
2639 if (!strtab)
2640 return 0;
2641
2642 DataExtractor rel_data;
2643 if (!ReadSectionData(rel_section, rel_data))
2644 return 0;
2645
2646 DataExtractor symtab_data;
2647 if (!ReadSectionData(symtab, symtab_data))
2648 return 0;
2649
2650 DataExtractor strtab_data;
2651 if (!ReadSectionData(strtab, strtab_data))
2652 return 0;
2653
2654 unsigned rel_type = PLTRelocationType();
2655 if (!rel_type)
2656 return 0;
2657
2658 return ParsePLTRelocations(symbol_table, start_id, rel_type, &m_header,
2659 rel_hdr, plt_hdr, sym_hdr, plt_section_sp,
2660 rel_data, symtab_data, strtab_data);
2661}
2662
Ed Masted13f6912017-10-02 14:35:07 +00002663unsigned ObjectFileELF::ApplyRelocations(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002664 Symtab *symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,
2665 const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr,
2666 DataExtractor &rel_data, DataExtractor &symtab_data,
2667 DataExtractor &debug_data, Section *rel_section) {
2668 ELFRelocation rel(rel_hdr->sh_type);
2669 lldb::addr_t offset = 0;
2670 const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
2671 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
2672 reloc_info_fn reloc_type;
2673 reloc_info_fn reloc_symbol;
2674
2675 if (hdr->Is32Bit()) {
2676 reloc_type = ELFRelocation::RelocType32;
2677 reloc_symbol = ELFRelocation::RelocSymbol32;
2678 } else {
2679 reloc_type = ELFRelocation::RelocType64;
2680 reloc_symbol = ELFRelocation::RelocSymbol64;
2681 }
2682
2683 for (unsigned i = 0; i < num_relocations; ++i) {
2684 if (rel.Parse(rel_data, &offset) == false)
2685 break;
2686
2687 Symbol *symbol = NULL;
2688
2689 if (hdr->Is32Bit()) {
2690 switch (reloc_type(rel)) {
2691 case R_386_32:
2692 case R_386_PC32:
2693 default:
Zachary Turnera6d54642017-12-02 00:15:29 +00002694 // FIXME: This asserts with this input:
2695 //
2696 // foo.cpp
2697 // int main(int argc, char **argv) { return 0; }
2698 //
2699 // clang++.exe --target=i686-unknown-linux-gnu -g -c foo.cpp -o foo.o
2700 //
2701 // and running this on the foo.o module.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002702 assert(false && "unexpected relocation type");
2703 }
2704 } else {
2705 switch (reloc_type(rel)) {
2706 case R_X86_64_64: {
2707 symbol = symtab->FindSymbolByID(reloc_symbol(rel));
2708 if (symbol) {
2709 addr_t value = symbol->GetAddressRef().GetFileAddress();
2710 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();
2711 uint64_t *dst = reinterpret_cast<uint64_t *>(
2712 data_buffer_sp->GetBytes() + rel_section->GetFileOffset() +
2713 ELFRelocation::RelocOffset64(rel));
2714 *dst = value + ELFRelocation::RelocAddend64(rel);
2715 }
2716 break;
2717 }
2718 case R_X86_64_32:
Stephane Sezer9e2fe8b2018-08-17 00:35:47 +00002719 case R_X86_64_32S:
2720 case R_AARCH64_ABS32: {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002721 symbol = symtab->FindSymbolByID(reloc_symbol(rel));
2722 if (symbol) {
2723 addr_t value = symbol->GetAddressRef().GetFileAddress();
2724 value += ELFRelocation::RelocAddend32(rel);
Stephane Sezer9e2fe8b2018-08-17 00:35:47 +00002725 if ((reloc_type(rel) == R_X86_64_32 && (value <= UINT32_MAX)) ||
Stephane Sezer0c679b72018-08-06 22:21:28 +00002726 (reloc_type(rel) == R_X86_64_32S &&
Stephane Sezer9e2fe8b2018-08-17 00:35:47 +00002727 ((int64_t)value <= INT32_MAX && (int64_t)value >= INT32_MIN)) ||
2728 (reloc_type(rel) == R_AARCH64_ABS32 && (value <= UINT32_MAX))) {
2729 Log *log =
2730 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES);
2731 log->Printf("Failed to apply debug info relocations");
2732 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002733 uint32_t truncated_addr = (value & 0xFFFFFFFF);
2734 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();
2735 uint32_t *dst = reinterpret_cast<uint32_t *>(
2736 data_buffer_sp->GetBytes() + rel_section->GetFileOffset() +
2737 ELFRelocation::RelocOffset32(rel));
2738 *dst = truncated_addr;
2739 }
2740 break;
2741 }
2742 case R_X86_64_PC32:
2743 default:
2744 assert(false && "unexpected relocation type");
2745 }
2746 }
2747 }
2748
2749 return 0;
2750}
2751
2752unsigned ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr,
Ed Masted13f6912017-10-02 14:35:07 +00002753 user_id_t rel_id,
2754 lldb_private::Symtab *thetab) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002755 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
2756
2757 // Parse in the section list if needed.
2758 SectionList *section_list = GetSectionList();
2759 if (!section_list)
2760 return 0;
2761
2762 // Section ID's are ones based.
2763 user_id_t symtab_id = rel_hdr->sh_link + 1;
2764 user_id_t debug_id = rel_hdr->sh_info + 1;
2765
2766 const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
2767 if (!symtab_hdr)
2768 return 0;
2769
2770 const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id);
2771 if (!debug_hdr)
2772 return 0;
2773
2774 Section *rel = section_list->FindSectionByID(rel_id).get();
2775 if (!rel)
2776 return 0;
2777
2778 Section *symtab = section_list->FindSectionByID(symtab_id).get();
2779 if (!symtab)
2780 return 0;
2781
2782 Section *debug = section_list->FindSectionByID(debug_id).get();
2783 if (!debug)
2784 return 0;
2785
2786 DataExtractor rel_data;
2787 DataExtractor symtab_data;
2788 DataExtractor debug_data;
2789
Ed Masted13f6912017-10-02 14:35:07 +00002790 if (GetData(rel->GetFileOffset(), rel->GetFileSize(), rel_data) &&
2791 GetData(symtab->GetFileOffset(), symtab->GetFileSize(), symtab_data) &&
2792 GetData(debug->GetFileOffset(), debug->GetFileSize(), debug_data)) {
2793 ApplyRelocations(thetab, &m_header, rel_hdr, symtab_hdr, debug_hdr,
2794 rel_data, symtab_data, debug_data, debug);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002795 }
2796
2797 return 0;
2798}
2799
2800Symtab *ObjectFileELF::GetSymtab() {
2801 ModuleSP module_sp(GetModule());
2802 if (!module_sp)
2803 return NULL;
2804
2805 // We always want to use the main object file so we (hopefully) only have one
Adrian Prantl05097242018-04-30 16:49:04 +00002806 // cached copy of our symtab, dynamic sections, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002807 ObjectFile *module_obj_file = module_sp->GetObjectFile();
2808 if (module_obj_file && module_obj_file != this)
2809 return module_obj_file->GetSymtab();
2810
2811 if (m_symtab_ap.get() == NULL) {
2812 SectionList *section_list = module_sp->GetSectionList();
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +00002813 if (!section_list)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002814 return NULL;
Ashok Thirumurthi35729bb2013-09-24 15:34:13 +00002815
Kate Stoneb9c1b512016-09-06 20:57:50 +00002816 uint64_t symbol_id = 0;
2817 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002818
Kate Stoneb9c1b512016-09-06 20:57:50 +00002819 // Sharable objects and dynamic executables usually have 2 distinct symbol
2820 // tables, one named ".symtab", and the other ".dynsym". The dynsym is a
Adrian Prantl05097242018-04-30 16:49:04 +00002821 // smaller version of the symtab that only contains global symbols. The
2822 // information found in the dynsym is therefore also found in the symtab,
2823 // while the reverse is not necessarily true.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002824 Section *symtab =
2825 section_list->FindSectionByType(eSectionTypeELFSymbolTable, true).get();
2826 if (!symtab) {
2827 // The symtab section is non-allocable and can be stripped, so if it
Adrian Prantl05097242018-04-30 16:49:04 +00002828 // doesn't exist then use the dynsym section which should always be
2829 // there.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002830 symtab =
2831 section_list->FindSectionByType(eSectionTypeELFDynamicSymbols, true)
2832 .get();
2833 }
2834 if (symtab) {
2835 m_symtab_ap.reset(new Symtab(symtab->GetObjectFile()));
2836 symbol_id += ParseSymbolTable(m_symtab_ap.get(), symbol_id, symtab);
2837 }
2838
2839 // DT_JMPREL
2840 // If present, this entry's d_ptr member holds the address of
2841 // relocation
2842 // entries associated solely with the procedure linkage table.
2843 // Separating
2844 // these relocation entries lets the dynamic linker ignore them during
2845 // process initialization, if lazy binding is enabled. If this entry is
2846 // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must
2847 // also be present.
2848 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
2849 if (symbol) {
2850 // Synthesize trampoline symbols to help navigate the PLT.
2851 addr_t addr = symbol->d_ptr;
2852 Section *reloc_section =
2853 section_list->FindSectionContainingFileAddress(addr).get();
2854 if (reloc_section) {
2855 user_id_t reloc_id = reloc_section->GetID();
2856 const ELFSectionHeaderInfo *reloc_header =
2857 GetSectionHeaderByIndex(reloc_id);
2858 assert(reloc_header);
2859
2860 if (m_symtab_ap == nullptr)
2861 m_symtab_ap.reset(new Symtab(reloc_section->GetObjectFile()));
2862
2863 ParseTrampolineSymbols(m_symtab_ap.get(), symbol_id, reloc_header,
2864 reloc_id);
2865 }
2866 }
2867
2868 DWARFCallFrameInfo *eh_frame = GetUnwindTable().GetEHFrameInfo();
2869 if (eh_frame) {
2870 if (m_symtab_ap == nullptr)
2871 m_symtab_ap.reset(new Symtab(this));
2872 ParseUnwindSymbols(m_symtab_ap.get(), eh_frame);
2873 }
2874
2875 // If we still don't have any symtab then create an empty instance to avoid
Adrian Prantl05097242018-04-30 16:49:04 +00002876 // do the section lookup next time.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002877 if (m_symtab_ap == nullptr)
2878 m_symtab_ap.reset(new Symtab(this));
2879
2880 m_symtab_ap->CalculateSymbolSizes();
2881 }
2882
Ed Masted13f6912017-10-02 14:35:07 +00002883 return m_symtab_ap.get();
2884}
2885
2886void ObjectFileELF::RelocateSection(lldb_private::Section *section)
2887{
Davide Italiano1e6a01f2018-05-12 01:25:48 +00002888 static const char *debug_prefix = ".debug";
Ed Masted13f6912017-10-02 14:35:07 +00002889
Adrian Prantl05097242018-04-30 16:49:04 +00002890 // Set relocated bit so we stop getting called, regardless of whether we
2891 // actually relocate.
Ed Masted13f6912017-10-02 14:35:07 +00002892 section->SetIsRelocated(true);
2893
2894 // We only relocate in ELF relocatable files
2895 if (CalculateType() != eTypeObjectFile)
2896 return;
2897
Davide Italiano1e6a01f2018-05-12 01:25:48 +00002898 const char *section_name = section->GetName().GetCString();
Ed Masted13f6912017-10-02 14:35:07 +00002899 // Can't relocate that which can't be named
Davide Italiano1e6a01f2018-05-12 01:25:48 +00002900 if (section_name == nullptr)
Ed Masted13f6912017-10-02 14:35:07 +00002901 return;
2902
2903 // We don't relocate non-debug sections at the moment
Davide Italiano1e6a01f2018-05-12 01:25:48 +00002904 if (strncmp(section_name, debug_prefix, strlen(debug_prefix)))
Ed Masted13f6912017-10-02 14:35:07 +00002905 return;
2906
2907 // Relocation section names to look for
Davide Italiano1e6a01f2018-05-12 01:25:48 +00002908 std::string needle = std::string(".rel") + section_name;
2909 std::string needlea = std::string(".rela") + section_name;
Ed Masted13f6912017-10-02 14:35:07 +00002910
Kate Stoneb9c1b512016-09-06 20:57:50 +00002911 for (SectionHeaderCollIter I = m_section_headers.begin();
2912 I != m_section_headers.end(); ++I) {
2913 if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL) {
Ed Masted13f6912017-10-02 14:35:07 +00002914 const char *hay_name = I->section_name.GetCString();
2915 if (hay_name == nullptr)
2916 continue;
2917 if (needle == hay_name || needlea == hay_name) {
2918 const ELFSectionHeader &reloc_header = *I;
2919 user_id_t reloc_id = SectionIndex(I);
2920 RelocateDebugSections(&reloc_header, reloc_id, GetSymtab());
2921 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002922 }
2923 }
2924 }
Tamas Berghammer6b63b142016-02-18 11:12:18 +00002925}
Tamas Berghammer5bfd4d02016-02-10 12:10:58 +00002926
Kate Stoneb9c1b512016-09-06 20:57:50 +00002927void ObjectFileELF::ParseUnwindSymbols(Symtab *symbol_table,
2928 DWARFCallFrameInfo *eh_frame) {
2929 SectionList *section_list = GetSectionList();
2930 if (!section_list)
2931 return;
2932
2933 // First we save the new symbols into a separate list and add them to the
Adrian Prantl05097242018-04-30 16:49:04 +00002934 // symbol table after we colleced all symbols we want to add. This is
Davide Italiano1e6a01f2018-05-12 01:25:48 +00002935 // neccessary because adding a new symbol invalidates the internal index of
Adrian Prantl05097242018-04-30 16:49:04 +00002936 // the symtab what causing the next lookup to be slow because it have to
2937 // recalculate the index first.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002938 std::vector<Symbol> new_symbols;
2939
2940 eh_frame->ForEachFDEEntries([this, symbol_table, section_list, &new_symbols](
2941 lldb::addr_t file_addr, uint32_t size, dw_offset_t) {
2942 Symbol *symbol = symbol_table->FindSymbolAtFileAddress(file_addr);
2943 if (symbol) {
2944 if (!symbol->GetByteSizeIsValid()) {
2945 symbol->SetByteSize(size);
2946 symbol->SetSizeIsSynthesized(true);
2947 }
2948 } else {
2949 SectionSP section_sp =
2950 section_list->FindSectionContainingFileAddress(file_addr);
2951 if (section_sp) {
2952 addr_t offset = file_addr - section_sp->GetFileAddress();
2953 const char *symbol_name = GetNextSyntheticSymbolName().GetCString();
2954 uint64_t symbol_id = symbol_table->GetNumSymbols();
2955 Symbol eh_symbol(
2956 symbol_id, // Symbol table index.
2957 symbol_name, // Symbol name.
2958 false, // Is the symbol name mangled?
2959 eSymbolTypeCode, // Type of this symbol.
2960 true, // Is this globally visible?
2961 false, // Is this symbol debug info?
2962 false, // Is this symbol a trampoline?
2963 true, // Is this symbol artificial?
2964 section_sp, // Section in which this symbol is defined or null.
2965 offset, // Offset in section or symbol value.
2966 0, // Size: Don't specify the size as an FDE can
2967 false, // Size is valid: cover multiple symbols.
2968 false, // Contains linker annotations?
2969 0); // Symbol flags.
2970 new_symbols.push_back(eh_symbol);
2971 }
2972 }
2973 return true;
2974 });
2975
2976 for (const Symbol &s : new_symbols)
2977 symbol_table->AddSymbol(s);
2978}
2979
2980bool ObjectFileELF::IsStripped() {
2981 // TODO: determine this for ELF
2982 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002983}
2984
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002985//===----------------------------------------------------------------------===//
2986// Dump
2987//
2988// Dump the specifics of the runtime file container (such as any headers
2989// segments, sections, etc).
2990//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00002991void ObjectFileELF::Dump(Stream *s) {
2992 ModuleSP module_sp(GetModule());
2993 if (!module_sp) {
2994 return;
2995 }
Adrian McCarthy543725c2016-04-04 21:21:49 +00002996
Kate Stoneb9c1b512016-09-06 20:57:50 +00002997 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
2998 s->Printf("%p: ", static_cast<void *>(this));
2999 s->Indent();
3000 s->PutCString("ObjectFileELF");
Adrian McCarthy543725c2016-04-04 21:21:49 +00003001
Kate Stoneb9c1b512016-09-06 20:57:50 +00003002 ArchSpec header_arch;
3003 GetArchitecture(header_arch);
Adrian McCarthy543725c2016-04-04 21:21:49 +00003004
Kate Stoneb9c1b512016-09-06 20:57:50 +00003005 *s << ", file = '" << m_file
3006 << "', arch = " << header_arch.GetArchitectureName() << "\n";
Adrian McCarthy543725c2016-04-04 21:21:49 +00003007
Kate Stoneb9c1b512016-09-06 20:57:50 +00003008 DumpELFHeader(s, m_header);
3009 s->EOL();
3010 DumpELFProgramHeaders(s);
3011 s->EOL();
3012 DumpELFSectionHeaders(s);
3013 s->EOL();
3014 SectionList *section_list = GetSectionList();
3015 if (section_list)
3016 section_list->Dump(s, NULL, true, UINT32_MAX);
3017 Symtab *symtab = GetSymtab();
3018 if (symtab)
3019 symtab->Dump(s, NULL, eSortOrderNone);
3020 s->EOL();
3021 DumpDependentModules(s);
3022 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003023}
3024
3025//----------------------------------------------------------------------
3026// DumpELFHeader
3027//
3028// Dump the ELF header to the specified output stream
3029//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003030void ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) {
3031 s->PutCString("ELF Header\n");
3032 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
3033 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG1],
3034 header.e_ident[EI_MAG1]);
3035 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG2],
3036 header.e_ident[EI_MAG2]);
3037 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG3],
3038 header.e_ident[EI_MAG3]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003039
Kate Stoneb9c1b512016-09-06 20:57:50 +00003040 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
3041 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
3042 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
3043 s->Printf("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
3044 s->Printf("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003045
Kate Stoneb9c1b512016-09-06 20:57:50 +00003046 s->Printf("e_type = 0x%4.4x ", header.e_type);
3047 DumpELFHeader_e_type(s, header.e_type);
3048 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
3049 s->Printf("e_version = 0x%8.8x\n", header.e_version);
3050 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry);
3051 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff);
3052 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff);
3053 s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
3054 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
3055 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
Pavel Labath23ccc292017-01-31 23:09:46 +00003056 s->Printf("e_phnum = 0x%8.8x\n", header.e_phnum);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003057 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
Pavel Labath23ccc292017-01-31 23:09:46 +00003058 s->Printf("e_shnum = 0x%8.8x\n", header.e_shnum);
3059 s->Printf("e_shstrndx = 0x%8.8x\n", header.e_shstrndx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003060}
3061
3062//----------------------------------------------------------------------
3063// DumpELFHeader_e_type
3064//
3065// Dump an token value for the ELF header member e_type
3066//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003067void ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) {
3068 switch (e_type) {
3069 case ET_NONE:
3070 *s << "ET_NONE";
3071 break;
3072 case ET_REL:
3073 *s << "ET_REL";
3074 break;
3075 case ET_EXEC:
3076 *s << "ET_EXEC";
3077 break;
3078 case ET_DYN:
3079 *s << "ET_DYN";
3080 break;
3081 case ET_CORE:
3082 *s << "ET_CORE";
3083 break;
3084 default:
3085 break;
3086 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003087}
3088
3089//----------------------------------------------------------------------
3090// DumpELFHeader_e_ident_EI_DATA
3091//
3092// Dump an token value for the ELF header member e_ident[EI_DATA]
3093//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003094void ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s,
3095 unsigned char ei_data) {
3096 switch (ei_data) {
3097 case ELFDATANONE:
3098 *s << "ELFDATANONE";
3099 break;
3100 case ELFDATA2LSB:
3101 *s << "ELFDATA2LSB - Little Endian";
3102 break;
3103 case ELFDATA2MSB:
3104 *s << "ELFDATA2MSB - Big Endian";
3105 break;
3106 default:
3107 break;
3108 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003109}
3110
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003111//----------------------------------------------------------------------
3112// DumpELFProgramHeader
3113//
3114// Dump a single ELF program header to the specified output stream
3115//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003116void ObjectFileELF::DumpELFProgramHeader(Stream *s,
3117 const ELFProgramHeader &ph) {
3118 DumpELFProgramHeader_p_type(s, ph.p_type);
3119 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset,
3120 ph.p_vaddr, ph.p_paddr);
3121 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz,
3122 ph.p_flags);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003123
Kate Stoneb9c1b512016-09-06 20:57:50 +00003124 DumpELFProgramHeader_p_flags(s, ph.p_flags);
3125 s->Printf(") %8.8" PRIx64, ph.p_align);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003126}
3127
3128//----------------------------------------------------------------------
3129// DumpELFProgramHeader_p_type
3130//
Adrian Prantl05097242018-04-30 16:49:04 +00003131// Dump an token value for the ELF program header member p_type which describes
3132// the type of the program header
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003133// ----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003134void ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) {
3135 const int kStrWidth = 15;
3136 switch (p_type) {
3137 CASE_AND_STREAM(s, PT_NULL, kStrWidth);
3138 CASE_AND_STREAM(s, PT_LOAD, kStrWidth);
3139 CASE_AND_STREAM(s, PT_DYNAMIC, kStrWidth);
3140 CASE_AND_STREAM(s, PT_INTERP, kStrWidth);
3141 CASE_AND_STREAM(s, PT_NOTE, kStrWidth);
3142 CASE_AND_STREAM(s, PT_SHLIB, kStrWidth);
3143 CASE_AND_STREAM(s, PT_PHDR, kStrWidth);
3144 CASE_AND_STREAM(s, PT_TLS, kStrWidth);
Filipe Cabecinhas477d86d2013-05-23 23:01:14 +00003145 CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003146 default:
3147 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
3148 break;
3149 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003150}
3151
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003152//----------------------------------------------------------------------
3153// DumpELFProgramHeader_p_flags
3154//
3155// Dump an token value for the ELF program header member p_flags
3156//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003157void ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) {
3158 *s << ((p_flags & PF_X) ? "PF_X" : " ")
3159 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
3160 << ((p_flags & PF_W) ? "PF_W" : " ")
3161 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
3162 << ((p_flags & PF_R) ? "PF_R" : " ");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003163}
3164
3165//----------------------------------------------------------------------
3166// DumpELFProgramHeaders
3167//
3168// Dump all of the ELF program header to the specified output stream
3169//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003170void ObjectFileELF::DumpELFProgramHeaders(Stream *s) {
3171 if (!ParseProgramHeaders())
3172 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003173
Kate Stoneb9c1b512016-09-06 20:57:50 +00003174 s->PutCString("Program Headers\n");
3175 s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
3176 "p_filesz p_memsz p_flags p_align\n");
3177 s->PutCString("==== --------------- -------- -------- -------- "
3178 "-------- -------- ------------------------- --------\n");
Ed Maste3a8ab6e2015-02-23 15:33:11 +00003179
Kate Stoneb9c1b512016-09-06 20:57:50 +00003180 uint32_t idx = 0;
3181 for (ProgramHeaderCollConstIter I = m_program_headers.begin();
3182 I != m_program_headers.end(); ++I, ++idx) {
3183 s->Printf("[%2u] ", idx);
3184 ObjectFileELF::DumpELFProgramHeader(s, *I);
3185 s->EOL();
3186 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003187}
3188
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003189//----------------------------------------------------------------------
3190// DumpELFSectionHeader
3191//
3192// Dump a single ELF section header to the specified output stream
3193//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003194void ObjectFileELF::DumpELFSectionHeader(Stream *s,
3195 const ELFSectionHeaderInfo &sh) {
3196 s->Printf("%8.8x ", sh.sh_name);
3197 DumpELFSectionHeader_sh_type(s, sh.sh_type);
3198 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags);
3199 DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
3200 s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr,
3201 sh.sh_offset, sh.sh_size);
3202 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
3203 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003204}
3205
3206//----------------------------------------------------------------------
3207// DumpELFSectionHeader_sh_type
3208//
3209// Dump an token value for the ELF section header member sh_type which
3210// describes the type of the section
3211//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003212void ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) {
3213 const int kStrWidth = 12;
3214 switch (sh_type) {
3215 CASE_AND_STREAM(s, SHT_NULL, kStrWidth);
3216 CASE_AND_STREAM(s, SHT_PROGBITS, kStrWidth);
3217 CASE_AND_STREAM(s, SHT_SYMTAB, kStrWidth);
3218 CASE_AND_STREAM(s, SHT_STRTAB, kStrWidth);
3219 CASE_AND_STREAM(s, SHT_RELA, kStrWidth);
3220 CASE_AND_STREAM(s, SHT_HASH, kStrWidth);
3221 CASE_AND_STREAM(s, SHT_DYNAMIC, kStrWidth);
3222 CASE_AND_STREAM(s, SHT_NOTE, kStrWidth);
3223 CASE_AND_STREAM(s, SHT_NOBITS, kStrWidth);
3224 CASE_AND_STREAM(s, SHT_REL, kStrWidth);
3225 CASE_AND_STREAM(s, SHT_SHLIB, kStrWidth);
3226 CASE_AND_STREAM(s, SHT_DYNSYM, kStrWidth);
3227 CASE_AND_STREAM(s, SHT_LOPROC, kStrWidth);
3228 CASE_AND_STREAM(s, SHT_HIPROC, kStrWidth);
3229 CASE_AND_STREAM(s, SHT_LOUSER, kStrWidth);
3230 CASE_AND_STREAM(s, SHT_HIUSER, kStrWidth);
3231 default:
3232 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
3233 break;
3234 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003235}
3236
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003237//----------------------------------------------------------------------
3238// DumpELFSectionHeader_sh_flags
3239//
3240// Dump an token value for the ELF section header member sh_flags
3241//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003242void ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s,
3243 elf_xword sh_flags) {
3244 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
3245 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
3246 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
3247 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
3248 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003249}
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003250
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003251//----------------------------------------------------------------------
3252// DumpELFSectionHeaders
3253//
3254// Dump all of the ELF section header to the specified output stream
3255//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003256void ObjectFileELF::DumpELFSectionHeaders(Stream *s) {
3257 if (!ParseSectionHeaders())
3258 return;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003259
Kate Stoneb9c1b512016-09-06 20:57:50 +00003260 s->PutCString("Section Headers\n");
3261 s->PutCString("IDX name type flags "
3262 "addr offset size link info addralgn "
3263 "entsize Name\n");
3264 s->PutCString("==== -------- ------------ -------------------------------- "
3265 "-------- -------- -------- -------- -------- -------- "
3266 "-------- ====================\n");
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003267
Kate Stoneb9c1b512016-09-06 20:57:50 +00003268 uint32_t idx = 0;
3269 for (SectionHeaderCollConstIter I = m_section_headers.begin();
3270 I != m_section_headers.end(); ++I, ++idx) {
3271 s->Printf("[%2u] ", idx);
3272 ObjectFileELF::DumpELFSectionHeader(s, *I);
3273 const char *section_name = I->section_name.AsCString("");
3274 if (section_name)
3275 *s << ' ' << section_name << "\n";
3276 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +00003277}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003278
Kate Stoneb9c1b512016-09-06 20:57:50 +00003279void ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) {
3280 size_t num_modules = ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003281
Kate Stoneb9c1b512016-09-06 20:57:50 +00003282 if (num_modules > 0) {
3283 s->PutCString("Dependent Modules:\n");
3284 for (unsigned i = 0; i < num_modules; ++i) {
3285 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
3286 s->Printf(" %s\n", spec.GetFilename().GetCString());
3287 }
3288 }
3289}
3290
3291bool ObjectFileELF::GetArchitecture(ArchSpec &arch) {
3292 if (!ParseHeader())
3293 return false;
3294
3295 if (m_section_headers.empty()) {
3296 // Allow elf notes to be parsed which may affect the detected architecture.
3297 ParseSectionHeaders();
3298 }
3299
3300 if (CalculateType() == eTypeCoreFile &&
3301 m_arch_spec.TripleOSIsUnspecifiedUnknown()) {
3302 // Core files don't have section headers yet they have PT_NOTE program
Adrian Prantl05097242018-04-30 16:49:04 +00003303 // headers that might shed more light on the architecture
Kate Stoneb9c1b512016-09-06 20:57:50 +00003304 if (ParseProgramHeaders()) {
Kamil Rytarowski12801f12017-03-26 15:34:57 +00003305 for (size_t i = 1, count = GetProgramHeaderCount(); i <= count; ++i) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003306 const elf::ELFProgramHeader *header = GetProgramHeaderByIndex(i);
3307 if (header && header->p_type == PT_NOTE && header->p_offset != 0 &&
3308 header->p_filesz > 0) {
3309 DataExtractor data;
3310 if (data.SetData(m_data, header->p_offset, header->p_filesz) ==
3311 header->p_filesz) {
3312 lldb_private::UUID uuid;
3313 RefineModuleDetailsFromNote(data, m_arch_spec, uuid);
3314 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003315 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003316 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003317 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003318 }
3319 arch = m_arch_spec;
3320 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003321}
3322
Kate Stoneb9c1b512016-09-06 20:57:50 +00003323ObjectFile::Type ObjectFileELF::CalculateType() {
3324 switch (m_header.e_type) {
3325 case llvm::ELF::ET_NONE:
3326 // 0 - No file type
Greg Clayton9e00b6a652011-07-09 00:41:34 +00003327 return eTypeUnknown;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003328
3329 case llvm::ELF::ET_REL:
3330 // 1 - Relocatable file
3331 return eTypeObjectFile;
3332
3333 case llvm::ELF::ET_EXEC:
3334 // 2 - Executable file
3335 return eTypeExecutable;
3336
3337 case llvm::ELF::ET_DYN:
3338 // 3 - Shared object file
3339 return eTypeSharedLibrary;
3340
3341 case ET_CORE:
3342 // 4 - Core file
3343 return eTypeCoreFile;
3344
3345 default:
3346 break;
3347 }
3348 return eTypeUnknown;
Greg Clayton9e00b6a652011-07-09 00:41:34 +00003349}
3350
Kate Stoneb9c1b512016-09-06 20:57:50 +00003351ObjectFile::Strata ObjectFileELF::CalculateStrata() {
3352 switch (m_header.e_type) {
3353 case llvm::ELF::ET_NONE:
3354 // 0 - No file type
Greg Clayton9e00b6a652011-07-09 00:41:34 +00003355 return eStrataUnknown;
Greg Clayton9e00b6a652011-07-09 00:41:34 +00003356
Kate Stoneb9c1b512016-09-06 20:57:50 +00003357 case llvm::ELF::ET_REL:
3358 // 1 - Relocatable file
3359 return eStrataUnknown;
3360
3361 case llvm::ELF::ET_EXEC:
3362 // 2 - Executable file
3363 // TODO: is there any way to detect that an executable is a kernel
Adrian Prantl05097242018-04-30 16:49:04 +00003364 // related executable by inspecting the program headers, section headers,
3365 // symbols, or any other flag bits???
Kate Stoneb9c1b512016-09-06 20:57:50 +00003366 return eStrataUser;
3367
3368 case llvm::ELF::ET_DYN:
3369 // 3 - Shared object file
3370 // TODO: is there any way to detect that an shared library is a kernel
Adrian Prantl05097242018-04-30 16:49:04 +00003371 // related executable by inspecting the program headers, section headers,
3372 // symbols, or any other flag bits???
Kate Stoneb9c1b512016-09-06 20:57:50 +00003373 return eStrataUnknown;
3374
3375 case ET_CORE:
3376 // 4 - Core file
3377 // TODO: is there any way to detect that an core file is a kernel
Adrian Prantl05097242018-04-30 16:49:04 +00003378 // related executable by inspecting the program headers, section headers,
3379 // symbols, or any other flag bits???
Kate Stoneb9c1b512016-09-06 20:57:50 +00003380 return eStrataUnknown;
3381
3382 default:
3383 break;
3384 }
3385 return eStrataUnknown;
3386}
Pavel Labathe2867bc2017-12-15 14:23:58 +00003387
3388size_t ObjectFileELF::ReadSectionData(Section *section,
3389 lldb::offset_t section_offset, void *dst,
3390 size_t dst_len) {
3391 // If some other objectfile owns this data, pass this to them.
3392 if (section->GetObjectFile() != this)
3393 return section->GetObjectFile()->ReadSectionData(section, section_offset,
3394 dst, dst_len);
3395
3396 if (!section->Test(SHF_COMPRESSED))
3397 return ObjectFile::ReadSectionData(section, section_offset, dst, dst_len);
3398
3399 // For compressed sections we need to read to full data to be able to
3400 // decompress.
3401 DataExtractor data;
3402 ReadSectionData(section, data);
3403 return data.CopyData(section_offset, dst_len, dst);
3404}
3405
3406size_t ObjectFileELF::ReadSectionData(Section *section,
3407 DataExtractor &section_data) {
3408 // If some other objectfile owns this data, pass this to them.
3409 if (section->GetObjectFile() != this)
3410 return section->GetObjectFile()->ReadSectionData(section, section_data);
3411
Pavel Labathe2867bc2017-12-15 14:23:58 +00003412 size_t result = ObjectFile::ReadSectionData(section, section_data);
3413 if (result == 0 || !section->Test(SHF_COMPRESSED))
3414 return result;
3415
3416 auto Decompressor = llvm::object::Decompressor::create(
3417 section->GetName().GetStringRef(),
3418 {reinterpret_cast<const char *>(section_data.GetDataStart()),
Pavel Labath4c2eb8b2017-12-15 14:39:12 +00003419 size_t(section_data.GetByteSize())},
Pavel Labathe2867bc2017-12-15 14:23:58 +00003420 GetByteOrder() == eByteOrderLittle, GetAddressByteSize() == 8);
3421 if (!Decompressor) {
Leonard Mosescu9ba51572018-08-07 18:00:30 +00003422 GetModule()->ReportWarning(
3423 "Unable to initialize decompressor for section '%s': %s",
3424 section->GetName().GetCString(),
3425 llvm::toString(Decompressor.takeError()).c_str());
3426 section_data.Clear();
3427 return 0;
Pavel Labathe2867bc2017-12-15 14:23:58 +00003428 }
Leonard Mosescu9ba51572018-08-07 18:00:30 +00003429
Pavel Labathe2867bc2017-12-15 14:23:58 +00003430 auto buffer_sp =
3431 std::make_shared<DataBufferHeap>(Decompressor->getDecompressedSize(), 0);
Leonard Mosescu9ba51572018-08-07 18:00:30 +00003432 if (auto error = Decompressor->decompress(
Pavel Labathe2867bc2017-12-15 14:23:58 +00003433 {reinterpret_cast<char *>(buffer_sp->GetBytes()),
Pavel Labath4c2eb8b2017-12-15 14:39:12 +00003434 size_t(buffer_sp->GetByteSize())})) {
Leonard Mosescu9ba51572018-08-07 18:00:30 +00003435 GetModule()->ReportWarning(
3436 "Decompression of section '%s' failed: %s",
3437 section->GetName().GetCString(),
3438 llvm::toString(std::move(error)).c_str());
3439 section_data.Clear();
3440 return 0;
Pavel Labathe2867bc2017-12-15 14:23:58 +00003441 }
Leonard Mosescu9ba51572018-08-07 18:00:30 +00003442
Pavel Labathe2867bc2017-12-15 14:23:58 +00003443 section_data.SetData(buffer_sp);
3444 return buffer_sp->GetByteSize();
3445}
Pavel Labath16064d32018-03-20 11:56:24 +00003446
3447bool ObjectFileELF::AnySegmentHasPhysicalAddress() {
3448 size_t header_count = ParseProgramHeaders();
3449 for (size_t i = 1; i <= header_count; ++i) {
3450 auto header = GetProgramHeaderByIndex(i);
3451 if (header->p_paddr != 0)
3452 return true;
3453 }
3454 return false;
3455}
3456
3457std::vector<ObjectFile::LoadableData>
3458ObjectFileELF::GetLoadableData(Target &target) {
Adrian Prantl05097242018-04-30 16:49:04 +00003459 // Create a list of loadable data from loadable segments, using physical
3460 // addresses if they aren't all null
Pavel Labath16064d32018-03-20 11:56:24 +00003461 std::vector<LoadableData> loadables;
3462 size_t header_count = ParseProgramHeaders();
3463 bool should_use_paddr = AnySegmentHasPhysicalAddress();
3464 for (size_t i = 1; i <= header_count; ++i) {
3465 LoadableData loadable;
3466 auto header = GetProgramHeaderByIndex(i);
3467 if (header->p_type != llvm::ELF::PT_LOAD)
3468 continue;
3469 loadable.Dest = should_use_paddr ? header->p_paddr : header->p_vaddr;
3470 if (loadable.Dest == LLDB_INVALID_ADDRESS)
3471 continue;
3472 if (header->p_filesz == 0)
3473 continue;
3474 auto segment_data = GetSegmentDataByIndex(i);
3475 loadable.Contents = llvm::ArrayRef<uint8_t>(segment_data.GetDataStart(),
3476 segment_data.GetByteSize());
3477 loadables.push_back(loadable);
3478 }
3479 return loadables;
3480}