forked from anza-xyz/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectFileELF.cpp
3727 lines (3263 loc) · 135 KB
/
ObjectFileELF.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===-- ObjectFileELF.cpp -------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ObjectFileELF.h"
#include <algorithm>
#include <cassert>
#include <optional>
#include <unordered_map>
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/Progress.h"
#include "lldb/Core/Section.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/LZMA.h"
#include "lldb/Symbol/DWARFCallFrameInfo.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Target/SectionLoadList.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/DataBufferHeap.h"
#include "lldb/Utility/FileSpecList.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/RangeMap.h"
#include "lldb/Utility/Status.h"
#include "lldb/Utility/Stream.h"
#include "lldb/Utility/Timer.h"
#include "llvm/ADT/IntervalMap.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/Object/Decompressor.h"
#include "llvm/Support/ARMBuildAttributes.h"
#include "llvm/Support/CRC.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/MipsABIFlags.h"
#define CASE_AND_STREAM(s, def, width) \
case def: \
s->Printf("%-*s", width, #def); \
break;
using namespace lldb;
using namespace lldb_private;
using namespace elf;
using namespace llvm::ELF;
LLDB_PLUGIN_DEFINE(ObjectFileELF)
// ELF note owner definitions
static const char *const LLDB_NT_OWNER_FREEBSD = "FreeBSD";
static const char *const LLDB_NT_OWNER_GNU = "GNU";
static const char *const LLDB_NT_OWNER_NETBSD = "NetBSD";
static const char *const LLDB_NT_OWNER_NETBSDCORE = "NetBSD-CORE";
static const char *const LLDB_NT_OWNER_OPENBSD = "OpenBSD";
static const char *const LLDB_NT_OWNER_ANDROID = "Android";
static const char *const LLDB_NT_OWNER_CORE = "CORE";
static const char *const LLDB_NT_OWNER_LINUX = "LINUX";
// ELF note type definitions
static const elf_word LLDB_NT_FREEBSD_ABI_TAG = 0x01;
static const elf_word LLDB_NT_FREEBSD_ABI_SIZE = 4;
static const elf_word LLDB_NT_GNU_ABI_TAG = 0x01;
static const elf_word LLDB_NT_GNU_ABI_SIZE = 16;
static const elf_word LLDB_NT_GNU_BUILD_ID_TAG = 0x03;
static const elf_word LLDB_NT_NETBSD_IDENT_TAG = 1;
static const elf_word LLDB_NT_NETBSD_IDENT_DESCSZ = 4;
static const elf_word LLDB_NT_NETBSD_IDENT_NAMESZ = 7;
static const elf_word LLDB_NT_NETBSD_PROCINFO = 1;
// GNU ABI note OS constants
static const elf_word LLDB_NT_GNU_ABI_OS_LINUX = 0x00;
static const elf_word LLDB_NT_GNU_ABI_OS_HURD = 0x01;
static const elf_word LLDB_NT_GNU_ABI_OS_SOLARIS = 0x02;
namespace {
//===----------------------------------------------------------------------===//
/// \class ELFRelocation
/// Generic wrapper for ELFRel and ELFRela.
///
/// This helper class allows us to parse both ELFRel and ELFRela relocation
/// entries in a generic manner.
class ELFRelocation {
public:
/// Constructs an ELFRelocation entry with a personality as given by @p
/// type.
///
/// \param type Either DT_REL or DT_RELA. Any other value is invalid.
ELFRelocation(unsigned type);
~ELFRelocation();
bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
static unsigned RelocType32(const ELFRelocation &rel);
static unsigned RelocType64(const ELFRelocation &rel);
static unsigned RelocSymbol32(const ELFRelocation &rel);
static unsigned RelocSymbol64(const ELFRelocation &rel);
static elf_addr RelocOffset32(const ELFRelocation &rel);
static elf_addr RelocOffset64(const ELFRelocation &rel);
static elf_sxword RelocAddend32(const ELFRelocation &rel);
static elf_sxword RelocAddend64(const ELFRelocation &rel);
bool IsRela() { return (reloc.is<ELFRela *>()); }
private:
typedef llvm::PointerUnion<ELFRel *, ELFRela *> RelocUnion;
RelocUnion reloc;
};
} // end anonymous namespace
ELFRelocation::ELFRelocation(unsigned type) {
if (type == DT_REL || type == SHT_REL)
reloc = new ELFRel();
else if (type == DT_RELA || type == SHT_RELA)
reloc = new ELFRela();
else {
assert(false && "unexpected relocation type");
reloc = static_cast<ELFRel *>(nullptr);
}
}
ELFRelocation::~ELFRelocation() {
if (reloc.is<ELFRel *>())
delete reloc.get<ELFRel *>();
else
delete reloc.get<ELFRela *>();
}
bool ELFRelocation::Parse(const lldb_private::DataExtractor &data,
lldb::offset_t *offset) {
if (reloc.is<ELFRel *>())
return reloc.get<ELFRel *>()->Parse(data, offset);
else
return reloc.get<ELFRela *>()->Parse(data, offset);
}
unsigned ELFRelocation::RelocType32(const ELFRelocation &rel) {
if (rel.reloc.is<ELFRel *>())
return ELFRel::RelocType32(*rel.reloc.get<ELFRel *>());
else
return ELFRela::RelocType32(*rel.reloc.get<ELFRela *>());
}
unsigned ELFRelocation::RelocType64(const ELFRelocation &rel) {
if (rel.reloc.is<ELFRel *>())
return ELFRel::RelocType64(*rel.reloc.get<ELFRel *>());
else
return ELFRela::RelocType64(*rel.reloc.get<ELFRela *>());
}
unsigned ELFRelocation::RelocSymbol32(const ELFRelocation &rel) {
if (rel.reloc.is<ELFRel *>())
return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel *>());
else
return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela *>());
}
unsigned ELFRelocation::RelocSymbol64(const ELFRelocation &rel) {
if (rel.reloc.is<ELFRel *>())
return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel *>());
else
return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela *>());
}
elf_addr ELFRelocation::RelocOffset32(const ELFRelocation &rel) {
if (rel.reloc.is<ELFRel *>())
return rel.reloc.get<ELFRel *>()->r_offset;
else
return rel.reloc.get<ELFRela *>()->r_offset;
}
elf_addr ELFRelocation::RelocOffset64(const ELFRelocation &rel) {
if (rel.reloc.is<ELFRel *>())
return rel.reloc.get<ELFRel *>()->r_offset;
else
return rel.reloc.get<ELFRela *>()->r_offset;
}
elf_sxword ELFRelocation::RelocAddend32(const ELFRelocation &rel) {
if (rel.reloc.is<ELFRel *>())
return 0;
else
return rel.reloc.get<ELFRela *>()->r_addend;
}
elf_sxword ELFRelocation::RelocAddend64(const ELFRelocation &rel) {
if (rel.reloc.is<ELFRel *>())
return 0;
else
return rel.reloc.get<ELFRela *>()->r_addend;
}
static user_id_t SegmentID(size_t PHdrIndex) {
return ~user_id_t(PHdrIndex);
}
bool ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset) {
// Read all fields.
if (data.GetU32(offset, &n_namesz, 3) == nullptr)
return false;
// The name field is required to be nul-terminated, and n_namesz includes the
// terminating nul in observed implementations (contrary to the ELF-64 spec).
// A special case is needed for cores generated by some older Linux versions,
// which write a note named "CORE" without a nul terminator and n_namesz = 4.
if (n_namesz == 4) {
char buf[4];
if (data.ExtractBytes(*offset, 4, data.GetByteOrder(), buf) != 4)
return false;
if (strncmp(buf, "CORE", 4) == 0) {
n_name = "CORE";
*offset += 4;
return true;
}
}
const char *cstr = data.GetCStr(offset, llvm::alignTo(n_namesz, 4));
if (cstr == nullptr) {
Log *log = GetLog(LLDBLog::Symbols);
LLDB_LOGF(log, "Failed to parse note name lacking nul terminator");
return false;
}
n_name = cstr;
return true;
}
static uint32_t sbfVariantFromElfFlags(const elf::ELFHeader &header) {
switch (header.e_flags) {
case llvm::ELF::EF_SBF_V0:
return ArchSpec::eSBFSubType_sbfv0;
case llvm::ELF::EF_SBF_V1:
return ArchSpec::eSBFSubType_sbfv1;
case llvm::ELF::EF_SBF_V2:
return ArchSpec::eSBFSubType_sbfv2;
case llvm::ELF::EF_SBF_V3:
return ArchSpec::eSBFSubType_sbfv3;
case llvm::ELF::EF_SBF_V4:
return ArchSpec::eSBFSubType_sbfv4;
default:
return ArchSpec::eSBFSubType_sbfv0;
}
}
static uint32_t mipsVariantFromElfFlags (const elf::ELFHeader &header) {
const uint32_t mips_arch = header.e_flags & llvm::ELF::EF_MIPS_ARCH;
uint32_t endian = header.e_ident[EI_DATA];
uint32_t arch_variant = ArchSpec::eMIPSSubType_unknown;
uint32_t fileclass = header.e_ident[EI_CLASS];
// If there aren't any elf flags available (e.g core elf file) then return
// default
// 32 or 64 bit arch (without any architecture revision) based on object file's class.
if (header.e_type == ET_CORE) {
switch (fileclass) {
case llvm::ELF::ELFCLASS32:
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el
: ArchSpec::eMIPSSubType_mips32;
case llvm::ELF::ELFCLASS64:
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el
: ArchSpec::eMIPSSubType_mips64;
default:
return arch_variant;
}
}
switch (mips_arch) {
case llvm::ELF::EF_MIPS_ARCH_1:
case llvm::ELF::EF_MIPS_ARCH_2:
case llvm::ELF::EF_MIPS_ARCH_32:
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el
: ArchSpec::eMIPSSubType_mips32;
case llvm::ELF::EF_MIPS_ARCH_32R2:
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r2el
: ArchSpec::eMIPSSubType_mips32r2;
case llvm::ELF::EF_MIPS_ARCH_32R6:
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r6el
: ArchSpec::eMIPSSubType_mips32r6;
case llvm::ELF::EF_MIPS_ARCH_3:
case llvm::ELF::EF_MIPS_ARCH_4:
case llvm::ELF::EF_MIPS_ARCH_5:
case llvm::ELF::EF_MIPS_ARCH_64:
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el
: ArchSpec::eMIPSSubType_mips64;
case llvm::ELF::EF_MIPS_ARCH_64R2:
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r2el
: ArchSpec::eMIPSSubType_mips64r2;
case llvm::ELF::EF_MIPS_ARCH_64R6:
return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r6el
: ArchSpec::eMIPSSubType_mips64r6;
default:
break;
}
return arch_variant;
}
static uint32_t riscvVariantFromElfFlags(const elf::ELFHeader &header) {
uint32_t fileclass = header.e_ident[EI_CLASS];
switch (fileclass) {
case llvm::ELF::ELFCLASS32:
return ArchSpec::eRISCVSubType_riscv32;
case llvm::ELF::ELFCLASS64:
return ArchSpec::eRISCVSubType_riscv64;
default:
return ArchSpec::eRISCVSubType_unknown;
}
}
static uint32_t ppc64VariantFromElfFlags(const elf::ELFHeader &header) {
uint32_t endian = header.e_ident[EI_DATA];
if (endian == ELFDATA2LSB)
return ArchSpec::eCore_ppc64le_generic;
else
return ArchSpec::eCore_ppc64_generic;
}
static uint32_t loongarchVariantFromElfFlags(const elf::ELFHeader &header) {
uint32_t fileclass = header.e_ident[EI_CLASS];
switch (fileclass) {
case llvm::ELF::ELFCLASS32:
return ArchSpec::eLoongArchSubType_loongarch32;
case llvm::ELF::ELFCLASS64:
return ArchSpec::eLoongArchSubType_loongarch64;
default:
return ArchSpec::eLoongArchSubType_unknown;
}
}
static uint32_t subTypeFromElfHeader(const elf::ELFHeader &header) {
if (header.e_machine == llvm::ELF::EM_MIPS)
return mipsVariantFromElfFlags(header);
else if (header.e_machine == llvm::ELF::EM_PPC64)
return ppc64VariantFromElfFlags(header);
else if (header.e_machine == llvm::ELF::EM_RISCV)
return riscvVariantFromElfFlags(header);
else if (header.e_machine == llvm::ELF::EM_LOONGARCH)
return loongarchVariantFromElfFlags(header);
else if (header.e_machine == llvm::ELF::EM_BPF)
return sbfVariantFromElfFlags(header);
else if (header.e_machine == llvm::ELF::EM_SBF)
return sbfVariantFromElfFlags(header);
return LLDB_INVALID_CPUTYPE;
}
char ObjectFileELF::ID;
// Arbitrary constant used as UUID prefix for core files.
const uint32_t ObjectFileELF::g_core_uuid_magic(0xE210C);
// Static methods.
void ObjectFileELF::Initialize() {
PluginManager::RegisterPlugin(GetPluginNameStatic(),
GetPluginDescriptionStatic(), CreateInstance,
CreateMemoryInstance, GetModuleSpecifications);
}
void ObjectFileELF::Terminate() {
PluginManager::UnregisterPlugin(CreateInstance);
}
ObjectFile *ObjectFileELF::CreateInstance(const lldb::ModuleSP &module_sp,
DataBufferSP data_sp,
lldb::offset_t data_offset,
const lldb_private::FileSpec *file,
lldb::offset_t file_offset,
lldb::offset_t length) {
bool mapped_writable = false;
if (!data_sp) {
data_sp = MapFileDataWritable(*file, length, file_offset);
if (!data_sp)
return nullptr;
data_offset = 0;
mapped_writable = true;
}
assert(data_sp);
if (data_sp->GetByteSize() <= (llvm::ELF::EI_NIDENT + data_offset))
return nullptr;
const uint8_t *magic = data_sp->GetBytes() + data_offset;
if (!ELFHeader::MagicBytesMatch(magic))
return nullptr;
// Update the data to contain the entire file if it doesn't already
if (data_sp->GetByteSize() < length) {
data_sp = MapFileDataWritable(*file, length, file_offset);
if (!data_sp)
return nullptr;
data_offset = 0;
mapped_writable = true;
magic = data_sp->GetBytes();
}
// If we didn't map the data as writable take ownership of the buffer.
if (!mapped_writable) {
data_sp = std::make_shared<DataBufferHeap>(data_sp->GetBytes(),
data_sp->GetByteSize());
data_offset = 0;
magic = data_sp->GetBytes();
}
unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
if (address_size == 4 || address_size == 8) {
std::unique_ptr<ObjectFileELF> objfile_up(new ObjectFileELF(
module_sp, data_sp, data_offset, file, file_offset, length));
ArchSpec spec = objfile_up->GetArchitecture();
if (spec && objfile_up->SetModulesArchitecture(spec))
return objfile_up.release();
}
return nullptr;
}
ObjectFile *ObjectFileELF::CreateMemoryInstance(
const lldb::ModuleSP &module_sp, WritableDataBufferSP data_sp,
const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {
if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT)) {
const uint8_t *magic = data_sp->GetBytes();
if (ELFHeader::MagicBytesMatch(magic)) {
unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
if (address_size == 4 || address_size == 8) {
std::unique_ptr<ObjectFileELF> objfile_up(
new ObjectFileELF(module_sp, data_sp, process_sp, header_addr));
ArchSpec spec = objfile_up->GetArchitecture();
if (spec && objfile_up->SetModulesArchitecture(spec))
return objfile_up.release();
}
}
}
return nullptr;
}
bool ObjectFileELF::MagicBytesMatch(DataBufferSP &data_sp,
lldb::addr_t data_offset,
lldb::addr_t data_length) {
if (data_sp &&
data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) {
const uint8_t *magic = data_sp->GetBytes() + data_offset;
return ELFHeader::MagicBytesMatch(magic);
}
return false;
}
static uint32_t calc_crc32(uint32_t init, const DataExtractor &data) {
return llvm::crc32(init,
llvm::ArrayRef(data.GetDataStart(), data.GetByteSize()));
}
uint32_t ObjectFileELF::CalculateELFNotesSegmentsCRC32(
const ProgramHeaderColl &program_headers, DataExtractor &object_data) {
uint32_t core_notes_crc = 0;
for (const ELFProgramHeader &H : program_headers) {
if (H.p_type == llvm::ELF::PT_NOTE) {
const elf_off ph_offset = H.p_offset;
const size_t ph_size = H.p_filesz;
DataExtractor segment_data;
if (segment_data.SetData(object_data, ph_offset, ph_size) != ph_size) {
// The ELF program header contained incorrect data, probably corefile
// is incomplete or corrupted.
break;
}
core_notes_crc = calc_crc32(core_notes_crc, segment_data);
}
}
return core_notes_crc;
}
static const char *OSABIAsCString(unsigned char osabi_byte) {
#define _MAKE_OSABI_CASE(x) \
case x: \
return #x
switch (osabi_byte) {
_MAKE_OSABI_CASE(ELFOSABI_NONE);
_MAKE_OSABI_CASE(ELFOSABI_HPUX);
_MAKE_OSABI_CASE(ELFOSABI_NETBSD);
_MAKE_OSABI_CASE(ELFOSABI_GNU);
_MAKE_OSABI_CASE(ELFOSABI_HURD);
_MAKE_OSABI_CASE(ELFOSABI_SOLARIS);
_MAKE_OSABI_CASE(ELFOSABI_AIX);
_MAKE_OSABI_CASE(ELFOSABI_IRIX);
_MAKE_OSABI_CASE(ELFOSABI_FREEBSD);
_MAKE_OSABI_CASE(ELFOSABI_TRU64);
_MAKE_OSABI_CASE(ELFOSABI_MODESTO);
_MAKE_OSABI_CASE(ELFOSABI_OPENBSD);
_MAKE_OSABI_CASE(ELFOSABI_OPENVMS);
_MAKE_OSABI_CASE(ELFOSABI_NSK);
_MAKE_OSABI_CASE(ELFOSABI_AROS);
_MAKE_OSABI_CASE(ELFOSABI_FENIXOS);
_MAKE_OSABI_CASE(ELFOSABI_C6000_ELFABI);
_MAKE_OSABI_CASE(ELFOSABI_C6000_LINUX);
_MAKE_OSABI_CASE(ELFOSABI_ARM);
_MAKE_OSABI_CASE(ELFOSABI_STANDALONE);
default:
return "<unknown-osabi>";
}
#undef _MAKE_OSABI_CASE
}
//
// WARNING : This function is being deprecated
// It's functionality has moved to ArchSpec::SetArchitecture This function is
// only being kept to validate the move.
//
// TODO : Remove this function
static bool GetOsFromOSABI(unsigned char osabi_byte,
llvm::Triple::OSType &ostype) {
switch (osabi_byte) {
case ELFOSABI_AIX:
ostype = llvm::Triple::OSType::AIX;
break;
case ELFOSABI_FREEBSD:
ostype = llvm::Triple::OSType::FreeBSD;
break;
case ELFOSABI_GNU:
ostype = llvm::Triple::OSType::Linux;
break;
case ELFOSABI_NETBSD:
ostype = llvm::Triple::OSType::NetBSD;
break;
case ELFOSABI_OPENBSD:
ostype = llvm::Triple::OSType::OpenBSD;
break;
case ELFOSABI_SOLARIS:
ostype = llvm::Triple::OSType::Solaris;
break;
default:
ostype = llvm::Triple::OSType::UnknownOS;
}
return ostype != llvm::Triple::OSType::UnknownOS;
}
size_t ObjectFileELF::GetModuleSpecifications(
const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
lldb::offset_t data_offset, lldb::offset_t file_offset,
lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
Log *log = GetLog(LLDBLog::Modules);
const size_t initial_count = specs.GetSize();
if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) {
DataExtractor data;
data.SetData(data_sp);
elf::ELFHeader header;
lldb::offset_t header_offset = data_offset;
if (header.Parse(data, &header_offset)) {
if (data_sp) {
ModuleSpec spec(file);
// In Android API level 23 and above, bionic dynamic linker is able to
// load .so file directly from zip file. In that case, .so file is
// page aligned and uncompressed, and this module spec should retain the
// .so file offset and file size to pass through the information from
// lldb-server to LLDB. For normal file, file_offset should be 0,
// length should be the size of the file.
spec.SetObjectOffset(file_offset);
spec.SetObjectSize(length);
const uint32_t sub_type = subTypeFromElfHeader(header);
spec.GetArchitecture().SetArchitecture(
eArchTypeELF, header.e_machine, sub_type, header.e_ident[EI_OSABI]);
if (spec.GetArchitecture().IsValid()) {
llvm::Triple::OSType ostype;
llvm::Triple::VendorType vendor;
llvm::Triple::OSType spec_ostype =
spec.GetArchitecture().GetTriple().getOS();
LLDB_LOGF(log, "ObjectFileELF::%s file '%s' module OSABI: %s",
__FUNCTION__, file.GetPath().c_str(),
OSABIAsCString(header.e_ident[EI_OSABI]));
// SetArchitecture should have set the vendor to unknown
vendor = spec.GetArchitecture().GetTriple().getVendor();
assert(vendor == llvm::Triple::UnknownVendor);
UNUSED_IF_ASSERT_DISABLED(vendor);
//
// Validate it is ok to remove GetOsFromOSABI
GetOsFromOSABI(header.e_ident[EI_OSABI], ostype);
assert(spec_ostype == ostype);
if (spec_ostype != llvm::Triple::OSType::UnknownOS) {
LLDB_LOGF(log,
"ObjectFileELF::%s file '%s' set ELF module OS type "
"from ELF header OSABI.",
__FUNCTION__, file.GetPath().c_str());
}
// When ELF file does not contain GNU build ID, the later code will
// calculate CRC32 with this data_sp file_offset and length. It is
// important for Android zip .so file, which is a slice of a file,
// to not access the outside of the file slice range.
if (data_sp->GetByteSize() < length)
data_sp = MapFileData(file, length, file_offset);
if (data_sp)
data.SetData(data_sp);
// In case there is header extension in the section #0, the header we
// parsed above could have sentinel values for e_phnum, e_shnum, and
// e_shstrndx. In this case we need to reparse the header with a
// bigger data source to get the actual values.
if (header.HasHeaderExtension()) {
lldb::offset_t header_offset = data_offset;
header.Parse(data, &header_offset);
}
uint32_t gnu_debuglink_crc = 0;
std::string gnu_debuglink_file;
SectionHeaderColl section_headers;
lldb_private::UUID &uuid = spec.GetUUID();
GetSectionHeaderInfo(section_headers, data, header, uuid,
gnu_debuglink_file, gnu_debuglink_crc,
spec.GetArchitecture());
llvm::Triple &spec_triple = spec.GetArchitecture().GetTriple();
LLDB_LOGF(log,
"ObjectFileELF::%s file '%s' module set to triple: %s "
"(architecture %s)",
__FUNCTION__, file.GetPath().c_str(),
spec_triple.getTriple().c_str(),
spec.GetArchitecture().GetArchitectureName());
if (!uuid.IsValid()) {
uint32_t core_notes_crc = 0;
if (!gnu_debuglink_crc) {
LLDB_SCOPED_TIMERF(
"Calculating module crc32 %s with size %" PRIu64 " KiB",
file.GetFilename().AsCString(),
(length - file_offset) / 1024);
// For core files - which usually don't happen to have a
// gnu_debuglink, and are pretty bulky - calculating whole
// contents crc32 would be too much of luxury. Thus we will need
// to fallback to something simpler.
if (header.e_type == llvm::ELF::ET_CORE) {
ProgramHeaderColl program_headers;
GetProgramHeaderInfo(program_headers, data, header);
core_notes_crc =
CalculateELFNotesSegmentsCRC32(program_headers, data);
} else {
gnu_debuglink_crc = calc_crc32(0, data);
}
}
using u32le = llvm::support::ulittle32_t;
if (gnu_debuglink_crc) {
// Use 4 bytes of crc from the .gnu_debuglink section.
u32le data(gnu_debuglink_crc);
uuid = UUID(&data, sizeof(data));
} else if (core_notes_crc) {
// Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make
// it look different form .gnu_debuglink crc followed by 4 bytes
// of note segments crc.
u32le data[] = {u32le(g_core_uuid_magic), u32le(core_notes_crc)};
uuid = UUID(data, sizeof(data));
}
}
specs.Append(spec);
}
}
}
}
return specs.GetSize() - initial_count;
}
// ObjectFile protocol
ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp,
DataBufferSP data_sp, lldb::offset_t data_offset,
const FileSpec *file, lldb::offset_t file_offset,
lldb::offset_t length)
: ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset) {
if (file)
m_file = *file;
}
ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp,
DataBufferSP header_data_sp,
const lldb::ProcessSP &process_sp,
addr_t header_addr)
: ObjectFile(module_sp, process_sp, header_addr, header_data_sp) {}
bool ObjectFileELF::IsExecutable() const {
return ((m_header.e_type & ET_EXEC) != 0) || (m_header.e_entry != 0);
}
bool ObjectFileELF::SetLoadAddress(Target &target, lldb::addr_t value,
bool value_is_offset) {
ModuleSP module_sp = GetModule();
if (module_sp) {
size_t num_loaded_sections = 0;
SectionList *section_list = GetSectionList();
if (section_list) {
if (!value_is_offset) {
addr_t base = GetBaseAddress().GetFileAddress();
if (base == LLDB_INVALID_ADDRESS)
return false;
value -= base;
}
const size_t num_sections = section_list->GetSize();
size_t sect_idx = 0;
for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
// Iterate through the object file sections to find all of the sections
// that have SHF_ALLOC in their flag bits.
SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
// PT_TLS segments can have the same p_vaddr and p_paddr as other
// PT_LOAD segments so we shouldn't load them. If we do load them, then
// the SectionLoadList will incorrectly fill in the instance variable
// SectionLoadList::m_addr_to_sect with the same address as a PT_LOAD
// segment and we won't be able to resolve addresses in the PT_LOAD
// segment whose p_vaddr entry matches that of the PT_TLS. Any variables
// that appear in the PT_TLS segments get resolved by the DWARF
// expressions. If this ever changes we will need to fix all object
// file plug-ins, but until then, we don't want PT_TLS segments to
// remove the entry from SectionLoadList::m_addr_to_sect when we call
// SetSectionLoadAddress() below.
if (section_sp->IsThreadSpecific())
continue;
if (section_sp->Test(SHF_ALLOC) ||
section_sp->GetType() == eSectionTypeContainer) {
lldb::addr_t load_addr = section_sp->GetFileAddress();
// We don't want to update the load address of a section with type
// eSectionTypeAbsoluteAddress as they already have the absolute load
// address already specified
if (section_sp->GetType() != eSectionTypeAbsoluteAddress)
load_addr += value;
// On 32-bit systems the load address have to fit into 4 bytes. The
// rest of the bytes are the overflow from the addition.
if (GetAddressByteSize() == 4)
load_addr &= 0xFFFFFFFF;
if (target.GetSectionLoadList().SetSectionLoadAddress(section_sp,
load_addr))
++num_loaded_sections;
}
}
return num_loaded_sections > 0;
}
}
return false;
}
ByteOrder ObjectFileELF::GetByteOrder() const {
if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
return eByteOrderBig;
if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
return eByteOrderLittle;
return eByteOrderInvalid;
}
uint32_t ObjectFileELF::GetAddressByteSize() const {
return m_data.GetAddressByteSize();
}
AddressClass ObjectFileELF::GetAddressClass(addr_t file_addr) {
Symtab *symtab = GetSymtab();
if (!symtab)
return AddressClass::eUnknown;
// The address class is determined based on the symtab. Ask it from the
// object file what contains the symtab information.
ObjectFile *symtab_objfile = symtab->GetObjectFile();
if (symtab_objfile != nullptr && symtab_objfile != this)
return symtab_objfile->GetAddressClass(file_addr);
auto res = ObjectFile::GetAddressClass(file_addr);
if (res != AddressClass::eCode)
return res;
auto ub = m_address_class_map.upper_bound(file_addr);
if (ub == m_address_class_map.begin()) {
// No entry in the address class map before the address. Return default
// address class for an address in a code section.
return AddressClass::eCode;
}
// Move iterator to the address class entry preceding address
--ub;
return ub->second;
}
size_t ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) {
return std::distance(m_section_headers.begin(), I);
}
size_t ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const {
return std::distance(m_section_headers.begin(), I);
}
bool ObjectFileELF::ParseHeader() {
lldb::offset_t offset = 0;
return m_header.Parse(m_data, &offset);
}
UUID ObjectFileELF::GetUUID() {
// Need to parse the section list to get the UUIDs, so make sure that's been
// done.
if (!ParseSectionHeaders() && GetType() != ObjectFile::eTypeCoreFile)
return UUID();
if (!m_uuid) {
using u32le = llvm::support::ulittle32_t;
if (GetType() == ObjectFile::eTypeCoreFile) {
uint32_t core_notes_crc = 0;
if (!ParseProgramHeaders())
return UUID();
core_notes_crc =
CalculateELFNotesSegmentsCRC32(m_program_headers, m_data);
if (core_notes_crc) {
// Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it
// look different form .gnu_debuglink crc - followed by 4 bytes of note
// segments crc.
u32le data[] = {u32le(g_core_uuid_magic), u32le(core_notes_crc)};
m_uuid = UUID(data, sizeof(data));
}
} else {
if (!m_gnu_debuglink_crc)
m_gnu_debuglink_crc = calc_crc32(0, m_data);
if (m_gnu_debuglink_crc) {
// Use 4 bytes of crc from the .gnu_debuglink section.
u32le data(m_gnu_debuglink_crc);
m_uuid = UUID(&data, sizeof(data));
}
}
}
return m_uuid;
}
std::optional<FileSpec> ObjectFileELF::GetDebugLink() {
if (m_gnu_debuglink_file.empty())
return std::nullopt;
return FileSpec(m_gnu_debuglink_file);
}
uint32_t ObjectFileELF::GetDependentModules(FileSpecList &files) {
size_t num_modules = ParseDependentModules();
uint32_t num_specs = 0;
for (unsigned i = 0; i < num_modules; ++i) {
if (files.AppendIfUnique(m_filespec_up->GetFileSpecAtIndex(i)))
num_specs++;
}
return num_specs;
}
Address ObjectFileELF::GetImageInfoAddress(Target *target) {
if (!ParseDynamicSymbols())
return Address();
SectionList *section_list = GetSectionList();
if (!section_list)
return Address();
// Find the SHT_DYNAMIC (.dynamic) section.
SectionSP dynsym_section_sp(
section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true));
if (!dynsym_section_sp)
return Address();
assert(dynsym_section_sp->GetObjectFile() == this);
user_id_t dynsym_id = dynsym_section_sp->GetID();
const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
if (!dynsym_hdr)
return Address();
for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) {
ELFDynamic &symbol = m_dynamic_symbols[i];
if (symbol.d_tag == DT_DEBUG) {
// Compute the offset as the number of previous entries plus the size of
// d_tag.
addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
return Address(dynsym_section_sp, offset);
}
// MIPS executables uses DT_MIPS_RLD_MAP_REL to support PIE. DT_MIPS_RLD_MAP
// exists in non-PIE.
else if ((symbol.d_tag == DT_MIPS_RLD_MAP ||
symbol.d_tag == DT_MIPS_RLD_MAP_REL) &&
target) {
addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target);
if (dyn_base == LLDB_INVALID_ADDRESS)
return Address();
Status error;
if (symbol.d_tag == DT_MIPS_RLD_MAP) {
// DT_MIPS_RLD_MAP tag stores an absolute address of the debug pointer.
Address addr;
if (target->ReadPointerFromMemory(dyn_base + offset, error, addr, true))
return addr;
}
if (symbol.d_tag == DT_MIPS_RLD_MAP_REL) {
// DT_MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,
// relative to the address of the tag.
uint64_t rel_offset;
rel_offset = target->ReadUnsignedIntegerFromMemory(
dyn_base + offset, GetAddressByteSize(), UINT64_MAX, error, true);
if (error.Success() && rel_offset != UINT64_MAX) {
Address addr;
addr_t debug_ptr_address =
dyn_base + (offset - GetAddressByteSize()) + rel_offset;
addr.SetOffset(debug_ptr_address);
return addr;
}
}
}
}
return Address();
}
lldb_private::Address ObjectFileELF::GetEntryPointAddress() {
if (m_entry_point_address.IsValid())
return m_entry_point_address;
if (!ParseHeader() || !IsExecutable())
return m_entry_point_address;
SectionList *section_list = GetSectionList();
addr_t offset = m_header.e_entry;
if (!section_list)
m_entry_point_address.SetOffset(offset);
else
m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
return m_entry_point_address;
}
Address ObjectFileELF::GetBaseAddress() {
if (GetType() == ObjectFile::eTypeObjectFile) {
for (SectionHeaderCollIter I = std::next(m_section_headers.begin());
I != m_section_headers.end(); ++I) {
const ELFSectionHeaderInfo &header = *I;
if (header.sh_flags & SHF_ALLOC)
return Address(GetSectionList()->FindSectionByID(SectionIndex(I)), 0);
}
return LLDB_INVALID_ADDRESS;
}
for (const auto &EnumPHdr : llvm::enumerate(ProgramHeaders())) {
const ELFProgramHeader &H = EnumPHdr.value();
if (H.p_type != PT_LOAD)
continue;
return Address(
GetSectionList()->FindSectionByID(SegmentID(EnumPHdr.index())), 0);
}
return LLDB_INVALID_ADDRESS;
}
// ParseDependentModules
size_t ObjectFileELF::ParseDependentModules() {
if (m_filespec_up)
return m_filespec_up->GetSize();
m_filespec_up = std::make_unique<FileSpecList>();