Skip to content

Commit

Permalink
Merge branch 'lsda_sym_minus_sym' into 'main'
Browse files Browse the repository at this point in the history
Check on both symbols in symbol_minus_symbol for END symbols

Closes #599 and debloating/dykondo#128

See merge request rewriting/ddisasm!1229
  • Loading branch information
jdorn-gt committed Nov 7, 2024
2 parents 2d92198 + 1327529 commit e09e352
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* Fix 16-Thumb STM instructions considered to be invalid if the same register
is used in reglist and register operands with writeback enabled.
* Fixed bug that could result in missed symbolic expressions with TLS variables for `local-executable` TLS model
* Fix bug that caused assembling error due to wrong `symbol_minus_symbol`
for lsda entries with references to the end of `.gcc_except_table`

# 1.9.0

Expand Down
18 changes: 18 additions & 0 deletions examples/asm_examples/ex_exceptions5/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CXX="g++"
EXEC=

all: ex_original.s
# NOTE: `-T linker-script.ld`
# This is to place .gcc_except_table *before* .note.gnu.build-id
# so that the reference label for the end of the section is aligned
# with the adjacent section. Otherwise, the next section can be some
# discarded one, which would prevent from demonstrating the issue.
#
$(CXX) -T linker-script.ld -o ex ex_original.s
@ $(EXEC) ./ex > out.txt
clean:
rm -f ex out.txt
rm -fr ex.unstripped *.old* dl_files *.gtirb
check:
@ $(EXEC) ./ex >/tmp/res.txt
@ diff out.txt /tmp/res.txt && echo TEST OK
67 changes: 67 additions & 0 deletions examples/asm_examples/ex_exceptions5/ex_original.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
.section .text

#-----------------------------------
.type foo, @function
foo:

.cfi_startproc
.cfi_lsda 27, .L_call_site_start
endbr64
pushq %rbp
movq %rsp,%rbp
nop
popq %rbp
retq
.cfi_endproc

# Entry point
.globl main
.type main, @function
main:
call callThrower # Call a function that may "throw" an exception
mov $60, %rax # syscall: exit
xor %rdi, %rdi # status: 0
syscall

# Dummy function simulating an exception thrower
.type callThrower, @function
callThrower:
ret # Simply return (replace with an actual throw in C++)

# Exception Handling Table
.section .gcc_except_table, "a", @progbits
.align 4

.L_entry_start:
.byte 0x1 # Entry indicating an exception
.byte 0x0
.byte 0x0
.byte 0x7d
.byte 0x0
.byte 0x0
.byte 0x0
.byte 0x0
.byte 0x0
.byte 0x0
.byte 0x0
.byte 0x0
.byte 0x0
.byte 0x0
.byte 0x0
.byte 0x0

.L_call_site_start:
.byte 0xff # Start of a call site
.byte 0xff # Additional indicator byte
.byte 0x1 # Additional indicator byte
#
# With this example, if a boundary_sym_expr is not correctly created
# for symbol_minus_symbol (either the first or the second symbol, or
# both) or an END symbol is not chosen for such a symbol,
# the assembler will fail with an error, such as
#
# "Error: invalid operands (.note.gnu.property and .gcc_except_table
# sections) for '-'"
#
.uleb128 .L_end - .L_end # Zero-length entry at the very end of the table
.L_end:
4 changes: 4 additions & 0 deletions examples/asm_examples/ex_exceptions5/linker-script.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SECTIONS {
.gcc_except_table : { *(.gcc_except_table) }
}
INSERT BEFORE .note.gnu.build-id;
18 changes: 9 additions & 9 deletions src/datalog/binary/elf/exceptions.dl
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,20 @@ labeled_ea(Personality):-
Same as `symbol_minus_symbol`: this is to avoid cyclic aggregation by
`boundary_sym_expr` using `symbol_minus_symbol`.
*/
.decl lsda_symbol_minus_symbol(EA:address,Size:unsigned,Symbol1:address,Symbol2:address,Scale:unsigned,Offset:number)
.decl lsda_symbol_minus_symbol(EA:address,Size:unsigned,Symbol1:address,Symbol2:address,Scale:unsigned,Offset:number,Reason:symbol)

symbol_minus_symbol(EA,Size,Symbol1,Symbol2,Scale,Offset):-
lsda_symbol_minus_symbol(EA,Size,Symbol1,Symbol2,Scale,Offset).
lsda_symbol_minus_symbol(EA,Size,Symbol1,Symbol2,Scale,Offset,_).

// If the second address is the end of the section, create `boundary_sym_expr`
// for the address.
boundary_sym_expr(EA, Dest):-
lsda_symbol_minus_symbol(EA,_,_,Dest,_,_),
lsda_symbol_minus_symbol(EA,_,_,Dest,_,_,_),
loaded_section(_,Dest,_).

// type table pointer
symbol_special_encoding(TypeTablePointerLocation,"uleb128"),
lsda_symbol_minus_symbol(TypeTablePointerLocation,Size,CallsiteTablePointerLoc-1,LsdaTypeTableAddress,1,0):-
lsda_symbol_minus_symbol(TypeTablePointerLocation,Size,CallsiteTablePointerLoc-1,LsdaTypeTableAddress,1,0,"type-table-pointer"):-
lsda_pointer_locations(Lsda,TypeTablePointerLocation,CallsiteTablePointerLoc),
lsda(Lsda,_,_,_,LsdaTypeTableAddress, _, _),
LsdaTypeTableAddress != 0,
Expand All @@ -198,7 +198,7 @@ lsda_symbol_minus_symbol(TypeTablePointerLocation,Size,CallsiteTablePointerLoc-1

//callsite table size
symbol_special_encoding(CallsiteTablePointerLoc,"uleb128"),
lsda_symbol_minus_symbol(CallsiteTablePointerLoc,Size,CallSiteTable_address,CallSiteTable_address+CallSiteTableLength,1,0):-
lsda_symbol_minus_symbol(CallsiteTablePointerLoc,Size,CallSiteTable_address,CallSiteTable_address+CallSiteTableLength,1,0,"callsite-table-size"):-
lsda_pointer_locations(Lsda,_,CallsiteTablePointerLoc),
lsda(Lsda,CallSiteTable_address,_,CallSiteTableLength,_, _, _),
Size = CallSiteTable_address - CallsiteTablePointerLoc,
Expand All @@ -217,23 +217,23 @@ symbol_special_encoding(EA_landingPad,EncodingName):-


//region beginning
lsda_symbol_minus_symbol(EA_start,Size,LandingPadBaseAddress,StartRegion,1,0):-
lsda_symbol_minus_symbol(EA_start,Size,LandingPadBaseAddress,StartRegion,1,0,"region-beginning"):-
lsda_callsite(CallSiteTable_address, EA_start, StartRegion,EA_end,_,_,_,_),
lsda(_,CallSiteTable_address,_,_,_,_,LandingPadBaseAddress),
Size = EA_end-EA_start,
Size > 0.

//region end
boundary_sym_expr(EA_end,EndRegion),
lsda_symbol_minus_symbol(EA_end,Size,StartRegion,EndRegion,1,0):-
lsda_symbol_minus_symbol(EA_end,Size,StartRegion,EndRegion,1,0,"region-end"):-
lsda_callsite(_, _, StartRegion,EA_end,EndRegion,EA_landingPad,_,_),
Size = EA_landingPad-EA_end,
Size > 0,
// Boundary is at the end of the block preceding the EndRegion label.
block_boundaries(_,_,EndRegion).

//landing pad
lsda_symbol_minus_symbol(EA_landingPad,Size,LandingPadBaseAddress,LandingPad,1,0):-
lsda_symbol_minus_symbol(EA_landingPad,Size,LandingPadBaseAddress,LandingPad,1,0,"landing-pad"):-
lsda_callsite(CallSiteTable_address, _, _,_,_,EA_landingPad,LandingPad,EA_endLandingPad),
lsda(_,CallSiteTable_address,_,_,_,_,LandingPadBaseAddress),
LandingPad != 0,
Expand All @@ -251,7 +251,7 @@ symbolic_data(EAType,EncodingSize,Type):-
EAType=LsdaTypeTableAddress-(EncodingSize*(Index+1)),
Type != 0.

lsda_symbol_minus_symbol(EAType,EncodingSize,EAType,Type,1,0):-
lsda_symbol_minus_symbol(EAType,EncodingSize,EAType,Type,1,0,"type-table"):-
lsda_type_entry(LsdaTypeTableAddress, Index,Type),
lsda(_,_,_,_,LsdaTypeTableAddress, TypeTableEncoding, _),
TypeTableEncoding band 0xF = Encoding,
Expand Down
6 changes: 5 additions & 1 deletion src/datalog/symbolization.dl
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,11 @@ symbolic_expr_symbol_minus_symbol(EA,Size,SymbolName1,SymbolName2,Scale,FinalOff
NewSymbol2 = Symbol2,
FinalOffset = Offset1
),
best_symexpr_symbol(NewSymbol1,SymbolName1,"Beg"),
(
NewSymbol1 = NewSymbol2, Symbol1Loc = Symbol2Loc;
NewSymbol1 != NewSymbol2, Symbol1Loc = "Beg"
),
best_symexpr_symbol(NewSymbol1,SymbolName1,Symbol1Loc),
(
boundary_sym_expr(EA,NewSymbol2), Symbol2Loc = "End"
;
Expand Down
9 changes: 7 additions & 2 deletions tests/linux-elf-x64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,11 @@ tests:
<<: *assembly
binary: fun.so

- name: ex_exceptions5
<<: *assembly

- name: ex_tls_local_exec
<<: *assembly
binary: ex

# ----------------------------------------------------------------------------
# Assembly examples. (stripped)
Expand Down Expand Up @@ -601,10 +603,13 @@ tests:
<<: *test-strip-default
binary: fun.so

- name: ex_exceptions5
<<: *assembly
<<: *test-strip-default

- name: ex_tls_local_exec
<<: *assembly
<<: *test-strip-default
binary: ex

# ----------------------------------------------------------------------------
# Relocatable ELF objects (.o).
Expand Down

0 comments on commit e09e352

Please sign in to comment.