mirror of
https://github.com/Noratrieb/elven-forest.git
synced 2026-01-14 10:45:03 +01:00
test data
This commit is contained in:
parent
94a6a0e999
commit
5de9ea38ca
7 changed files with 85 additions and 14 deletions
|
|
@ -2,8 +2,8 @@ use std::{fmt::Display, fs::File};
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use elven_parser::{
|
use elven_parser::{
|
||||||
consts::{self as c, DynamicTag, ShType, RX86_64},
|
consts::{self as c, DynamicTag, ShType, SymbolVisibility, RX86_64},
|
||||||
defs::{Addr, Elf},
|
defs::{Addr, Elf, Sym, SymInfo},
|
||||||
ElfParseError,
|
ElfParseError,
|
||||||
};
|
};
|
||||||
use memmap2::Mmap;
|
use memmap2::Mmap;
|
||||||
|
|
@ -31,6 +31,16 @@ struct SectionTable {
|
||||||
offset: u64,
|
offset: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Tabled)]
|
||||||
|
struct SymbolTable {
|
||||||
|
name: String,
|
||||||
|
info: SymInfo,
|
||||||
|
other: SymbolVisibility,
|
||||||
|
section: String,
|
||||||
|
value: Addr,
|
||||||
|
size: u64,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Tabled)]
|
#[derive(Tabled)]
|
||||||
struct RelaTable {
|
struct RelaTable {
|
||||||
section: String,
|
section: String,
|
||||||
|
|
@ -92,6 +102,34 @@ fn print_file(path: &str) -> anyhow::Result<()> {
|
||||||
|
|
||||||
print_table(Table::new(sections));
|
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");
|
println!("\nRelocations");
|
||||||
|
|
||||||
let relas = elf
|
let relas = elf
|
||||||
|
|
@ -101,12 +139,7 @@ fn print_file(path: &str) -> anyhow::Result<()> {
|
||||||
|
|
||||||
let sym = elf.symbol(rela.info.sym())?;
|
let sym = elf.symbol(rela.info.sym())?;
|
||||||
|
|
||||||
let symbol = if sym.info.r#type() == c::STT_SECTION {
|
let symbol = sym_display_name(elf, sym)?;
|
||||||
elf.sh_string(elf.section_header(sym.shndx)?.name)?
|
|
||||||
.to_string()
|
|
||||||
} else {
|
|
||||||
elf.string(sym.name)?.to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
let offset = Addr(rela.offset.0);
|
let offset = Addr(rela.offset.0);
|
||||||
let r#type = c::RX86_64(rela.info.r#type());
|
let r#type = c::RX86_64(rela.info.r#type());
|
||||||
|
|
@ -139,6 +172,15 @@ fn print_file(path: &str) -> anyhow::Result<()> {
|
||||||
Ok(())
|
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) {
|
fn print_table(mut table: Table) {
|
||||||
table.with(Style::blank());
|
table.with(Style::blank());
|
||||||
println!("{table}");
|
println!("{table}");
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ define_idx! {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A raw ELF. Does not come with cute ears for now.
|
/// A raw ELF. Does not come with cute ears for now.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct Elf<'a> {
|
pub struct Elf<'a> {
|
||||||
pub data: &'a [u8],
|
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)]
|
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct Rel {
|
pub struct Rel {
|
||||||
|
|
|
||||||
12
test_data/calls_obj.c
Normal file
12
test_data/calls_obj.c
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int uwu()
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
printf("%d", uwu());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -8,6 +8,9 @@ for rust_file in $SCRIPT_DIR/*.rs; do
|
||||||
done
|
done
|
||||||
|
|
||||||
for c_obj_file in $SCRIPT_DIR/*_obj.c; do
|
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).o"
|
||||||
cc "$c_obj_file" -c -o "$SCRIPT_DIR/out/$(basename $c_obj_file .c)"
|
done
|
||||||
|
|
||||||
|
for asm_file in $SCRIPT_DIR/*.asm; do
|
||||||
|
nasm "$asm_file" -felf64 -o "$SCRIPT_DIR/out/$(basename $asm_file .asm).o"
|
||||||
done
|
done
|
||||||
|
|
|
||||||
7
test_data/empty.asm
Normal file
7
test_data/empty.asm
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
global _start
|
||||||
|
|
||||||
|
section .text
|
||||||
|
_start:
|
||||||
|
mov rax, 60
|
||||||
|
mov rdi, 0
|
||||||
|
syscall
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include<stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
puts("Hello, World!");
|
puts("Hello, World!");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue