summary refs log tree commit diff
path: root/src/structs.rs
blob: 099e5eeb813df456fa8a6c64e0e6b523d820f33b (plain)
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
use bitflags::bitflags;

#[repr(C, packed)]
#[derive(Debug, Copy, Clone)]
pub struct ElfIdent {
    /// Magic number - 0x7F, then 'ELF' in ASCII
    pub magic: [u8; 4],

    /// 1 = 32 bit, 2 = 64 bit
    pub class: Class,

    // 1 = little endian, 2 = big endian
    pub data: Endian,

    /// ELF header version
    pub version: u8,

    /// OS ABI - usually 0 for System V
    pub OSABI: OSABI,

    // This field is used to distinguish among incompatible versions of an ABI.
    pub ABIVersion: u8,

    /// Unused/padding
    pub _unused: [u8; 7],
}

impl std::fmt::Display for ElfIdent {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("  Magic:   ")?;

        for b in unsafe { *(std::ptr::addr_of!(self.magic) as *const [u8; 16]) } {
            formatter.pad(&format!("{:02x?} ", b))?;
        }

        formatter.write_str("\n")?;

        formatter.write_fmt(format_args!(
            "  Class:                             {:?}\n",
            self.class
        ))?;
        formatter.write_fmt(format_args!(
            "  Data:                              {}\n",
            self.data
        ))?;
        formatter.write_fmt(format_args!(
            "  Version:                           {} ({})\n",
            self.version,
            match self.version {
                0 => "invalid",
                1 => "current",
                2_u8..=u8::MAX => todo!(), // need to cover all cases
            }
        ))?;
        formatter.write_fmt(format_args!(
            "  OS/ABI:                            {}\n",
            self.OSABI
        ))?;
        formatter.write_fmt(format_args!(
            "  ABI Version:                       {}\n",
            self.ABIVersion
        ))
    }
}

// https://man7.org/linux/man-pages/man5/elf.5.html
// https://wiki.osdev.org/ELF
// https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
#[repr(C)]
#[derive(Debug)]
pub struct ElfHeader {
    pub ident: ElfIdent,

    /// 1 = relocatable, 2 = executable, 3 = shared, 4 = core
    pub r#type: Type,

    /// Instruction set - see table below
    pub machine: Machine,

    /// ELF Version
    pub version: u32,

    /// Program entry position (virtual)
    pub entry: u64,

    /// Program header table position (bytes into file)
    pub phoff: u64,

    /// Section header table position (bytes into file)
    pub shoff: u64,

    /// This member holds processor-specific flags associated with the file
    /// Currently, no flags have been defined.
    pub flags: u32,

    /// Header size
    pub ehsize: u16,

    /// Size of an entry in the program header table
    pub phentsize: u16,

    /// Number of entries in the program header table
    pub phnum: u16,

    /// Size of an entry in the section header table
    pub shentsize: u16,

    /// Number of entries in the section header table
    pub shnum: u16,

    /// Index in section header table with the section names
    pub shstrndx: u16,
}

impl std::fmt::Display for ElfHeader {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        // yes, this is really what they do in readelf.c
        formatter.write_fmt(format_args!("{}", self.ident))?;
        formatter.write_fmt(format_args!(
            "  Type:                              {}\n",
            self.r#type
        ))?;
        formatter.write_fmt(format_args!(
            "  Machine:                           {}\n",
            self.machine
        ))?;
        formatter.write_fmt(format_args!(
            "  Version:                           {:#02x?}\n",
            self.version
        ))?;
        formatter.write_fmt(format_args!(
            "  Entry point address:               {:#x?}\n",
            self.entry
        ))?;
        formatter.write_fmt(format_args!(
            "  Start of program headers:          {} (bytes into file)\n",
            self.phoff
        ))?;
        formatter.write_fmt(format_args!(
            "  Start of section headers:          {} (bytes into file)\n",
            self.shoff
        ))?;
        formatter.write_fmt(format_args!(
            "  Flags:                             {:#x?}\n",
            self.flags
        ))?;
        formatter.write_fmt(format_args!(
            "  Size of this header:               {} (bytes)\n",
            self.ehsize
        ))?;
        formatter.write_fmt(format_args!(
            "  Size of program headers:           {} (bytes)\n",
            self.phentsize
        ))?;
        formatter.write_fmt(format_args!(
            "  Number of program headers:         {}\n",
            self.phnum
        ))?;
        formatter.write_fmt(format_args!(
            "  Size of section headers:           {} (bytes)\n",
            self.shentsize
        ))?;
        formatter.write_fmt(format_args!(
            "  Number of section headers:         {}\n",
            self.shnum
        ))?;
        formatter.write_fmt(format_args!(
            "  Section header string table index: {}\n",
            self.shstrndx
        ))
    }
}

#[derive(Debug, Copy, Clone)]
#[repr(u16)]
pub enum Type {
    None = 0,
    Rel = 1,
    Exec = 2,
    Dyn = 3,
    Core = 4,
}

impl std::fmt::Display for Type {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Type::None => formatter.write_str("NONE (None)"),
            Type::Rel => formatter.write_str("REL (Relocatable file)"),
            Type::Exec => formatter.write_str("EXEC (Executable file)"),
            Type::Dyn => formatter.write_str("DYN (Shared object file)"),
            Type::Core => formatter.write_str("CORE (Core file)"),
        }
    }
}

#[derive(Debug, Copy, Clone)]
#[repr(u16)]
pub enum Machine {
    // there are many many many more, im just gonna do x86
    // https://github.com/bminor/binutils-gdb/blob/master/binutils/readelf.c#L2746
    None = 1,
    X86_64 = 62,
}

impl std::fmt::Display for Machine {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Machine::None => formatter.write_str("None"),
            Machine::X86_64 => formatter.write_str("Advanced Micro Devices X86-64"),
        }
    }
}

#[derive(Debug, Copy, Clone)]
#[repr(u8)]
pub enum Class {
    ELF32 = 1,
    ELF64 = 2,
}

#[derive(Debug, Copy, Clone)]
#[repr(u8)]
pub enum OSABI {
    None = 0x00,
    SystemV = 0x01,
    // HPUX = 0x02,
    // Solaris = 0x03,
    // IRIX = 0x04,
    // FreeBSD = 0x05,
    // TRU64 = 0x06,
    // ARM = 0x07,
    Standalone = 0x08,
}

impl std::fmt::Display for OSABI {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            OSABI::None => formatter.write_str("UNIX - System V"),
            OSABI::SystemV => formatter.write_str("UNIX - System V"),
            OSABI::Standalone => formatter.write_str("Standalone"),
        }
    }
}

#[derive(Debug, Copy, Clone)]
#[repr(u8)]
pub enum Endian {
    Little = 1,
    Big = 2,
}

impl std::fmt::Display for Endian {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Endian::Little => formatter.write_str("2's complement, little endian"),
            Endian::Big => formatter.write_str("1's complement, big endian"),
        }
    }
}

#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub struct ElfSectionHeader {
    /*  This member specifies the name of the section.  Its value
    is an index into the section header string table section,
    giving the location of a null-terminated string. */
    pub name: u32,
    /*  This member categorizes the section's contents and
    semantics. */
    pub r#type: ElfSectionType,

    /*  Sections support one-bit flags that describe miscellaneous
    attributes.  If a flag bit is set in sh_flags, the
    attribute is "on" for the section.  Otherwise, the
    attribute is "off" or does not apply.  Undefined
    attributes are set to zero. */
    pub flags: ElfSectionFlags,

    /*  If this section appears in the memory image of a process,
    this member holds the address at which the section's first
    byte should reside.  Otherwise, the member contains zero. */
    pub addr: u64,

    /*  This member's value holds the byte offset from the
    beginning of the file to the first byte in the section.
    One section type, SHT_NOBITS, occupies no space in the
    file, and its sh_offset member locates the conceptual
    placement in the file. */
    pub offset: u64,

    /* 	This member holds the section's size in bytes.  Unless the
    section type is SHT_NOBITS, the section occupies sh_size
    bytes in the file.  A section of type SHT_NOBITS may have
    a nonzero size, but it occupies no space in the file. */
    pub size: u64,

    /* 	This member holds a section header table index link, whose
    interpretation depends on the section type. */
    pub link: u32,

    /* 	This member holds extra information, whose interpretation
    depends on the section type. */
    pub info: u32,

    /* 	Some sections have address alignment constraints.  If a
    section holds a doubleword, the system must ensure
    doubleword alignment for the entire section.  That is, the
    value of sh_addr must be congruent to zero, modulo the
    value of sh_addralign.  Only zero and positive integral
    powers of two are allowed.  The value 0 or 1 means that
    the section has no alignment constraints. */
    pub addralign: u64,

    /*	Some sections hold a table of fixed-sized entries, such as
    a symbol table.  For such a section, this member gives the
    size in bytes for each entry.  This member contains zero
    if the section does not hold a table of fixed-size
    entries. */
    pub entsize: u64,
}

impl std::fmt::Display for ElfSectionHeader {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_fmt(format_args!(
            "{: <16} {:0>16x} {:0>8x}\n       {:0>16x} {:0>16x} {: ^10} {: <5} {: <5} {}",
            self.r#type,
            self.addr,
            self.offset,
            self.size,
            self.entsize,
            self.flags,
            self.link,
            self.info,
            self.addralign
        ))?;
        Ok(())
    }
}

#[derive(Debug, Copy, Clone)]
#[repr(u32)]
pub enum ElfSectionType {
    /*  This value marks the section header as inactive.
    It does not have an associated section.  Other
    members of the section header have undefined
    values. */
    Null = 0,

    /*  This section holds information defined by the
    program, whose format and meaning are determined
    solely by the program. */
    ProgBits = 1,

    /*  This section holds a symbol table.  Typically,
    SHT_SYMTAB provides symbols for link editing,
    though it may also be used for dynamic linking.  As
    a complete symbol table, it may contain many
    symbols unnecessary for dynamic linking.  An object
    file can also contain a SHT_DYNSYM section. */
    SymTab = 2,

    /*  This section holds a string table.  An object file
    may have multiple string table sections. */
    StrTab = 3,

    /*  This section holds relocation entries with explicit
    addends, such as type Elf32_Rela for the 32-bit
    class of object files.  An object may have multiple
    relocation sections. */
    Rela = 4,

    /*  This section holds a symbol hash table.  An object
    participating in dynamic linking must contain a
    symbol hash table.  An object file may have only
    one hash table. */
    Hash = 5,

    /*  This section holds information for dynamic linking.
    An object file may have only one dynamic section. */
    Dynamic = 6,

    /* This section holds notes (ElfN_Nhdr). */
    Note = 7,

    /*  A section of this type occupies no space in the
    file but otherwise resembles SHT_PROGBITS.
    Although this section contains no bytes, the
    sh_offset member contains the conceptual file
    offset. */
    NoBits = 8,

    /*  This section holds relocation offsets without
    explicit addends, such as type Elf32_Rel for the
    32-bit class of object files.  An object file may
    have multiple relocation sections. */
    Rel = 9,

    /*  This section is reserved but has unspecified
    semantics. */
    ShLib = 10,

    /*  This section holds a minimal set of dynamic linking
    symbols.  An object file can also contain a
    SHT_SYMTAB section. */
    DynSym = 11,

    InitArray = 14,
    FiniArray = 15,

    GnuHash = 0x6FFFFFF6,
    VerNeed = 0x6FFFFFFE,
    VerSym = 0x6FFFFFFF,
}

impl std::fmt::Display for ElfSectionType {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            ElfSectionType::Null => "NULL".fmt(formatter),
            ElfSectionType::ProgBits => "PROGBITS".fmt(formatter),
            ElfSectionType::SymTab => "SYMTAB".fmt(formatter),
            ElfSectionType::StrTab => "STRTAB".fmt(formatter),
            ElfSectionType::Rela => "RELA".fmt(formatter),
            ElfSectionType::Hash => "HASH".fmt(formatter),
            ElfSectionType::Dynamic => "Dynamic".fmt(formatter),
            ElfSectionType::Note => "NOTE".fmt(formatter),
            ElfSectionType::NoBits => "NOBITS".fmt(formatter),
            ElfSectionType::Rel => "REL".fmt(formatter),
            ElfSectionType::ShLib => "SHLIB".fmt(formatter),
            ElfSectionType::DynSym => "DYNSYM".fmt(formatter),
            ElfSectionType::InitArray => "INIT_ARRAY".fmt(formatter),
            ElfSectionType::FiniArray => "FINI_ARRAY".fmt(formatter),
            ElfSectionType::GnuHash => "GNU_HASH".fmt(formatter),
            ElfSectionType::VerNeed => "VERNEED".fmt(formatter),
            ElfSectionType::VerSym => "VERSYM".fmt(formatter),
        }
    }
}

bitflags! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct ElfSectionFlags: u64 {
        const WRITE = 0x1;
        const ALLOC = 0x2;
        const EXEC = 0x4;
    }
}

// PAIN IN THE ASS!!!!!!!
// https://github.com/aixoss/binutils/blob/master/binutils/readelf.c#L4966
// https://github.com/llvm-mirror/llvm/blob/2c4ca6832fa6b306ee6a7010bfb80a3f2596f824/tools/llvm-readobj/ELFDumper.cpp#L1176
impl std::fmt::Display for ElfSectionFlags {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        "  A".fmt(formatter);
        Ok(())
    }
}