Repository navigation
Expand file tree
/
Copy pathelf_read.h
More file actions
122 lines (105 loc) · 2.82 KB
/
Copy pathelf_read.h
File metadata and controls
122 lines (105 loc) · 2.82 KB
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
#ifndef RAT_RATL_ELFREAD_H
#define RAT_RATL_ELFREAD_H
#include "core.h"
#include "elf_file.h"
#include <iosfwd>
namespace rat {
namespace detail {
U8 classify(U32 type, U64 flags);
B32
parseShdrs(const List<U8>& img, List<elf::SectionHeader>& out, const U8*& shstr, String& err);
B32 readAt(std::ifstream& f, U64 offset, U64 n, List<U8>& out);
String probeHostLoader();
List<String> probeHostLibDirs();
} // namespace detail
enum Bucket {
BText, // ALLOC|EXECINSTR
BRodata, // ALLOC ro
BEhFrame, // own section so .eh_frame_hdr can index it
BInitArray,
BFiniArray,
BData, // ALLOC|WRITE progbits
BBss, // ALLOC|WRITE nobits
BTdata, // tls progbits
BTbss, // tls nobits
BucketCount,
BDebug = 0xfe, // non-alloc .debug_*, merged per name
BNone = 0xff
};
struct InSection {
String name;
U64 align = 1;
U64 fileOff = 0; // offset in image
U64 size = 0;
U8 bucket = BNone;
U64 outOff = 0; // offset in bucket, filled at layout
U32 debugOut = 0; // index in Linker::debugOuts
B32 keep = false;
};
struct InSym {
String name;
U32 shndx = 0; // section index or SHN_*
U64 value = 0;
U64 size = 0;
U8 type = elf::STT_NOTYPE;
U8 bind = elf::STB_LOCAL;
B32 undef = true;
B32 common = false;
B32 abs = false;
};
struct InRel {
U32 secIdx = 0; // patched section
U64 offset = 0;
U32 sym = 0;
U32 type = 0;
I64 addend = 0;
};
struct InObject {
String path;
List<U8> image; // whole file, keeps section bytes valid
List<InSection> sections;
List<InSym> syms;
List<InRel> rels;
};
struct ArchiveFile {
String path;
List<U8> data;
Map<String, U64> index; // symbol -> member header offset
Set<U64> pulled;
};
// shared lib, resident only for its dynsym
struct Lib {
String soname;
List<U8> dynsym;
List<U8> dynstr;
};
// external symbol from a shared lib
struct Import {
enum Kind {
Func,
Data
} kind;
U32 dynIndex = 0; // index in .dynsym
U64 addr = 0; // PLT stub (Func) or .bss COPY (Data)
U64 size = 0;
U32 pltIndex = 0;
};
// GOT slot for a GOTPCREL-family reloc
struct GotSlot {
B32 isImport = false; // needs GLOB_DAT
U32 dynIndex = 0; // imports
U64 addr = 0; // defined targets, filled at write
};
B32 readWhole(const String& path, List<U8>& out);
B32 loadObject(
List<U8> img, const String& path, InObject& obj, Set<String>& seenGroups, String& err);
U64 arMemberSize(const List<U8>& d, U64 hdrOff);
// bytes start with the archive magic
ArchiveFile parseArchive(const String& path, List<U8> bytes);
B32 loadLibrary(const String& path, Lib& lib, String& err);
B32 findLibrary(const String& l, const List<String>& paths, String& found);
// host loader + lib dirs probed from ratl's own image, used when opt empty
const String& hostLoader();
const List<String>& hostLibDirs();
} // namespace rat
#endif