test data

This commit is contained in:
nora 2023-02-12 13:45:51 +01:00
parent 94a6a0e999
commit 5de9ea38ca
7 changed files with 85 additions and 14 deletions

View file

@ -2,8 +2,8 @@ use std::{fmt::Display, fs::File};
use anyhow::Context;
use elven_parser::{
consts::{self as c, DynamicTag, ShType, RX86_64},
defs::{Addr, Elf},
consts::{self as c, DynamicTag, ShType, SymbolVisibility, RX86_64},
defs::{Addr, Elf, Sym, SymInfo},
ElfParseError,
};
use memmap2::Mmap;
@ -31,6 +31,16 @@ struct SectionTable {
offset: u64,
}
#[derive(Tabled)]
struct SymbolTable {
name: String,
info: SymInfo,
other: SymbolVisibility,
section: String,
value: Addr,
size: u64,
}
#[derive(Tabled)]
struct RelaTable {
section: String,
@ -92,6 +102,34 @@ fn print_file(path: &str) -> anyhow::Result<()> {
print_table(Table::new(sections));
println!("\nSymbols");
let symbols = elf
.symbols()?
.iter()
.map(|sym| {
let name = sym_display_name(elf, sym)?;
let section = match sym.shndx.0 {
c::SHN_ABS => " ".to_string(),
c::SHN_COMMON => "".to_string(),
_ => elf
.sh_string(elf.section_header(sym.shndx)?.name)?
.to_string(),
};
Ok(SymbolTable {
name,
info: sym.info,
other: sym.other,
section,
size: sym.size,
value: sym.value,
})
})
.collect::<Result<Vec<_>, ElfParseError>>()?;
print_table(Table::new(symbols));
println!("\nRelocations");
let relas = elf
@ -101,12 +139,7 @@ fn print_file(path: &str) -> anyhow::Result<()> {
let sym = elf.symbol(rela.info.sym())?;
let symbol = if sym.info.r#type() == c::STT_SECTION {
elf.sh_string(elf.section_header(sym.shndx)?.name)?
.to_string()
} else {
elf.string(sym.name)?.to_string()
};
let symbol = sym_display_name(elf, sym)?;
let offset = Addr(rela.offset.0);
let r#type = c::RX86_64(rela.info.r#type());
@ -139,6 +172,15 @@ fn print_file(path: &str) -> anyhow::Result<()> {
Ok(())
}
fn sym_display_name(elf: Elf<'_>, sym: &Sym) -> Result<String, ElfParseError> {
Ok(if sym.info.r#type() == c::STT_SECTION {
elf.sh_string(elf.section_header(sym.shndx)?.name)?
.to_string()
} else {
elf.string(sym.name)?.to_string()
})
}
fn print_table(mut table: Table) {
table.with(Style::blank());
println!("{table}");

View file

@ -56,7 +56,7 @@ define_idx! {
}
/// A raw ELF. Does not come with cute ears for now.
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct Elf<'a> {
pub data: &'a [u8],
}
@ -153,6 +153,12 @@ impl Debug for SymInfo {
}
}
impl Display for SymInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?},{:?}", self.r#type(), self.binding())
}
}
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
#[repr(C)]
pub struct Rel {

12
test_data/calls_obj.c Normal file
View file

@ -0,0 +1,12 @@
#include <stdio.h>
int uwu()
{
return 1;
}
int main(int argc, char **argv)
{
printf("%d", uwu());
return 0;
}

View file

@ -8,6 +8,9 @@ for rust_file in $SCRIPT_DIR/*.rs; do
done
for c_obj_file in $SCRIPT_DIR/*_obj.c; do
echo $c_obj_file
cc "$c_obj_file" -c -o "$SCRIPT_DIR/out/$(basename $c_obj_file .c)"
cc "$c_obj_file" -c -o "$SCRIPT_DIR/out/$(basename $c_obj_file .c).o"
done
for asm_file in $SCRIPT_DIR/*.asm; do
nasm "$asm_file" -felf64 -o "$SCRIPT_DIR/out/$(basename $asm_file .asm).o"
done

7
test_data/empty.asm Normal file
View file

@ -0,0 +1,7 @@
global _start
section .text
_start:
mov rax, 60
mov rdi, 0
syscall

View file

@ -1,6 +1,7 @@
#include <stdio.h>
int main(int argc, char **argv) {
int main(int argc, char **argv)
{
puts("Hello, World!");
return 0;
}