Skip to content

Commit a4734a8

Browse files
committed
gtirb usages
1 parent d3550cf commit a4734a8

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

_posts/Assembly/2025-10-28-pyelftools-usages.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,15 @@ published: false
88

99
> Problem: I need to find all the references of all the entries of the GOT, and convert the instruction with their references with the correct labels --> normalization.
1010
11+
> Problem: there are different types of the data loading in the arm assembly file, so I need to identify them and try to map all the corresponding symbols to them.
12+
13+
14+
1. Mapping from the symbols to different data
15+
- Some data are in
16+
- .got section -->
17+
- .data section -->
18+
- .bss section --> NOBITS
19+
20+
21+
the call to the rel.plt and rel.dyn.
22+
the call routine? from call site to plt stub (in plt section), and then in plt stub call to real rel.plt in .got

_posts/Compiler/2025-10-21-Gtirb-investigation.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,3 +631,79 @@ void ArmPrettyPrinter::printOpIndirect(
631631
}
632632
```
633633

634+
## how to print the function calls
635+
636+
Here are some code snippets about how the functions at different sections are printed.
637+
- In rel.plt section, this is more special, because the symbols in rel.plt will be firstly accessed by plt_stubs in .plt seciton, then calls to .got section, but here is directly mapped to the rel.plt's symbol (ProxyBlock)
638+
```assembly
639+
add r0, pc, r0
640+
bl fwrite
641+
.arm
642+
643+
mov r0, #1
644+
bl exit
645+
```
646+
- In .text
647+
```assembly
648+
ldr r0, .L_10c04
649+
mov r1, r4
650+
ldr r0, [pc, r0]
651+
bl print_path
652+
.arm
653+
654+
mov r0, #10
655+
bl putchar
656+
```
657+
### Overview
658+
The pretty-printer does not compute symbols from raw immediates. It reads GTIRB symbolic expressions attached to the ByteInterval and prints symbol names when those expressions exist.
659+
660+
1. Instruction operand handling
661+
- Entry: ArmPrettyPrinter::printOperand(...)
662+
- Action: fetches the ByteInterval symbolic expression for the instruction address:
663+
block.getByteInterval()->getSymbolicExpression(ea - *block.getByteInterval()->getAddress())
664+
665+
2. Check for symbolic immediate
666+
- Entry: ArmPrettyPrinter::printOpImmediate(...)
667+
- Action: calls getSymbolicImmediate(symbolic) to see if the operand is a gtirb::SymAddrConst.
668+
669+
3. Print symbolic expression (symbol + addend)
670+
- Entry: PrettyPrinterBase::printSymbolicExpression(...)
671+
- Action: prints the symbol reference (sexpr->Sym) and any addend.
672+
673+
4. Convert gtirb::Symbol* → text
674+
- Entry: PrettyPrinterBase::printSymbolReference(...)
675+
- Action: handles forwarded symbols, skip policy, or delegates to getSymbolName(...) to format the final name.
676+
677+
5. Final formatting & ambiguous-symbol handling
678+
- Entry: PrettyPrinterBase::getSymbolName(...)
679+
- Action: formats and applies any disambiguation/renaming.
680+
681+
#### Find the rel.plt functions
682+
1. For printing the rel.plt functions, such as the `exit` function in the previous example, the general step is like:
683+
- a. Instruction → symbolic expression lookup: Fetch the symbolic expression at the instruction address.
684+
- b. Symbolic expression contains a gtirb::SymAddrConst referencing a symbol (e.g., a PLT stub like FUN_106c8). At the address at `bl #imm`, `#imm` is the address of the plt stub in the plt section, there is no symbol name, it is only a general CodeBlock.
685+
- c. Forwarding check (this is the key): queries the SymbolForwarding auxdata via aux_data::getForwardedSymbol(Symbol) if want to find the real name, which is in the related ProxyBlock.
686+
- d. Use forwarded name if present: If a forwarded symbol is found, the pretty-printer prints the forwarded symbol name (e.g., printf) instead of the stub name. If the forwarded name is skipped by policy it may print 0 and append a warning comment instead.
687+
688+
### Where the symbols come from
689+
The gtirb::SymAddrConst attached to the ByteInterval is created before printing (e.g., by GTIRB emitters, ELF→GTIRB conversion, relocation processing, or other analysis tools). The pretty-printer only reads this GTIRB metadata.
690+
691+
- Helpful function pointers (files)
692+
- ArmPrettyPrinter.cpp
693+
- ArmPrettyPrinter::printOperand(...)
694+
- ArmPrettyPrinter::printOpImmediate(...)
695+
- PrettyPrinter.cpp
696+
- PrettyPrinterBase::getSymbolicImmediate(...)
697+
- PrettyPrinterBase::printSymbolicExpression(...)
698+
- PrettyPrinterBase::printSymbolReference(...)
699+
- PrettyPrinterBase::getSymbolName(...)
700+
- PrettyPrinterBase::computeFunctionInformation(...) — builds mappings from auxdata (function names/blocks)
701+
- Edge cases / notes
702+
- If no SymAddrConst exists, the immediate is printed as a raw number.
703+
- If a symbol is skipped by policy, the printer may output 0 and append a warning comment.
704+
- The mapping of an immediate address → gtirb::Symbol is produced outside the pretty-printer (GTIRB/auxdata or the importer).
705+
706+
# Gtirb Functions (Python Version)
707+
708+
- Found blocks based on the addresses: `Module(AuxDataContainer).code/data_blocks_on/at(self, addrs: typing.Union[int, range]) -> typing.Iterable[DataBlock]`
709+
- Found edges that are in/out of the current `CfgNode` (CodeBlock): `out_edges(self, node: CfgNode) -> Iterator[Edge]`, `in_edges(self, node: CfgNode) -> Iterator[Edge]`

0 commit comments

Comments
 (0)