Skip to content

Commit

Permalink
Merge branch 'aseitz/mips-got-ofst' into 'main'
Browse files Browse the repository at this point in the history
Fix symbolic expression attributes for got_ofst

Closes #598

See merge request rewriting/ddisasm!1191
  • Loading branch information
aeflores committed Mar 20, 2024
2 parents 8f90f23 + 4f756ae commit 619b2f0
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
constant address.
* Refactor the code inference point system. Decouple heuristics from their weights.
Heuristic weights can now be modified by providing user hints.

* Generate GOT, PAGE and GOT, OFST symbolic expression attributes for split
.got loads on MIPS.

# 1.8.0

Expand Down
10 changes: 10 additions & 0 deletions examples/mips_asm_examples/ex_got_ofst/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.PHONY: clean check
out.txt: ex
@qemu-mips -L /usr/mips-linux-gnu $^ 2 > $@
ex: ex_original.s
mips-linux-gnu-gcc -no-pie -o $@ $^ -lm
clean:
rm -f ex out.txt ex.gtirb
check: ex
qemu-mips -L /usr/mips-linux-gnu $^ 2 > /tmp/res.txt
@ diff out.txt /tmp/res.txt && echo TEST OK
72 changes: 72 additions & 0 deletions examples/mips_asm_examples/ex_got_ofst/ex_original.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
.set noreorder
.set noat

.text

.global _start
_start:
lui $gp,%hi(_gp)
addiu $gp,$gp,%lo(_gp)
move $ra,$zero
lw $a0,%got(main)($gp)
lw $a1,0($sp)
addiu $a2,$sp,4
addiu $at,$zero,-8
and $sp,$sp,$at
addiu $sp,$sp,-32
lw $a3,%got(__libc_csu_init)($gp)
lw $t0,%got(__libc_csu_fini)($gp)
sw $t0,16($sp)
sw $v0,20($sp)
sw $sp,24($sp)
lw $t9,%got(__libc_start_main)($gp)
jalr $t9
nop

.globl main
.type main, @function
main:
addiu $sp,$sp,-32
sw $ra,28($sp)

lui $gp,%hi(_gp)
addiu $gp,$gp,%lo(_gp)

# call fun via got with split load
lw $t9,%got_page(fun)($gp)
addiu $t9,$t9,%got_ofst(fun)
jalr $t9
nop

move $v0, $zero

lw $ra,28($sp)
jr $ra
addiu $sp,$sp,32

.globl fun
.type fun, @function
fun:
addiu $sp,$sp,-32
sw $ra,28($sp)

# puts("hello world")

lui $v0,%hi(message)
addiu $a0,$v0,%lo(message)

lui $gp,%hi(_gp)
addiu $gp,$gp,%lo(_gp)
lw $v0,%got(puts)($gp)
move $t9,$v0
jalr $t9
nop

lw $ra,28($sp)
addiu $sp,$sp,32
jr $ra
nop

.section .rodata
message:
.string "Hello world"
10 changes: 6 additions & 4 deletions src/datalog/arch/mips_symbolization.dl
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ discarded_split_load(EA_hi,EA_lo,Dest_addr,Type,Points):-
// e.g., lw v0, -32696(gp) <-- v0: MIPS_BASE_ADDRESS
// addiu a0, v0, 2560 <-- 2560: offset from MIPS_BASE_ADDRESS to target L
// ->
// lw v0, %got(L)(gp)
// addiu a0, v0, %lo(L)
// lw v0, %got_page(L)(gp)
// addiu a0, v0, %got_ofst(L)
base_addr_offset_operand(EA,NextEA,Dest_addr):-
gp_relative_operand(EA,1,_),
value_reg(EA,Reg,_,"NONE",_,Value,_),
Expand Down Expand Up @@ -348,10 +348,12 @@ base_addr_offset_operand(EA,NextEA,Dest_addr):-
Dest_addr >= Begin, Dest_addr <= End
).

symbolic_operand_attribute(EA,1,"GOT"):-
symbolic_operand_attribute(EA,1,"GOT"),
symbolic_operand_attribute(EA,1,"PAGE"):-
base_addr_offset_operand(EA,_,_).

symbolic_operand_attribute(EA,2,"LO"):-
symbolic_operand_attribute(EA,2,"GOT"),
symbolic_operand_attribute(EA,2,"OFST"):-
base_addr_offset_operand(_,EA,_).

// Hi
Expand Down
2 changes: 2 additions & 0 deletions src/passes/Disassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ gtirb::SymAttributeSet buildSymbolicExpressionAttributes(
{"TPOFF", gtirb::SymAttribute::TPOFF},
{"DTPOFF", gtirb::SymAttribute::DTPOFF},
{"NTPOFF", gtirb::SymAttribute::NTPOFF},
{"PAGE", gtirb::SymAttribute::PAGE},
{"TLSGD", gtirb::SymAttribute::TLSGD},
{"TLSLD", gtirb::SymAttribute::TLSLD},
{"TLSLDM", gtirb::SymAttribute::TLSLDM},
Expand All @@ -519,6 +520,7 @@ gtirb::SymAttributeSet buildSymbolicExpressionAttributes(
// MIPS
{"HI", gtirb::SymAttribute::HI},
{"LO", gtirb::SymAttribute::LO},
{"OFST", gtirb::SymAttribute::OFST},
// X86
{"INDNTPOFF", gtirb::SymAttribute::INDNTPOFF},
};
Expand Down
12 changes: 11 additions & 1 deletion tests/qemu-elf-mips32.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ default: &default
binary: ex

# Compilation configuration.
build:
build: &default-build
c: ["mips-linux-gnu-gcc"]
cpp: ["mips-linux-gnu-g++"]
optimizations: ["-O0", "-O1", "-O2", "-O3", "-Os"]
Expand All @@ -27,6 +27,13 @@ default: &default
cfg_checks:
- dangling_auxdata

assembly: &assembly
<<: *default
path: examples/mips_asm_examples
build: &assembly-build
<<: *default-build
optimizations: [""]

test-strip-default: &test-strip-default
test:
wrapper: "qemu-mips -L /usr/mips-linux-gnu"
Expand Down Expand Up @@ -295,3 +302,6 @@ tests:
compiler: "mips-linux-gnu-g++"
flags: ["-no-pie"]
skip: false

- name: ex_got_ofst
<<: *assembly

0 comments on commit 619b2f0

Please sign in to comment.