0

After I compile my own program, I want to filter out all relative addresses in the output file. I have already disassembled the output file into readable assembly code, but I'm rather new to assembly and have considered two ways of obtaining the location of all relative addresses:

  1. Parse the assembly further by looking up each instruction and checking if it could contain a relative address. If it does, determine its location and so on.

  2. Instruct the compiler to highlight the relative addresses, such as by inserting nops before and after each relative address. Then, parse the assembly by examining the positions of the nops.

The first option seemed difficult, because of the vast number of possible assembly instructions in x86 (x64/x32) and ARM made it potentially time-consuming and forgetting any case or misinterpreting any instruction could lead to errors.

I'm uncertain if the second option is realistically achievable or if it would involve less effort than the first one. I have never attempted to customize a compiler before, but I came across this way of adding nops before every function, though not before relative addresses.

Therefore, my questions are:

Is the second option realistically feasible? Are there any other approaches that I haven't considered?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 2
    You want to include relative jumps like x86 `j*` / `call` and `loop` instructions? Relative data addressing is easy to find; in AT&T syntax, it will include `(%rip)`, which won't appear as a substring in any instruction that doesn't use a RIP-relative addressing mode. Option (2) doesn't sound feasible at all, unless you're writing your own compiler or assembler. – Peter Cordes Jun 14 '23 at 04:02
  • Yep, I also want to include relative jumps and loop instructions. I know about rip but that would be only a small percentage of all relative addresses used. Still thanks for your insights! – OrangeGutanus Jun 14 '23 at 04:19
  • You could look for a database of instructions for ones with a relative operand, like the XML file included with XED (which uops.info uses: https://uops.info/xml.html). Or Intel's vol.2 PDF manual includes a `rel8` or `rel32` in the table of instruction encodings for all that include a relative branch (conditional or otherwise), including obscure ones like `xbegin` transactional-memory; https://www.felixcloutier.com/x86/xbegin I assume you're specifically interested in x86 and maybe ARM, but you didn't tag anything so maybe you're hoping for a method portable across ISAs? – Peter Cordes Jun 14 '23 at 05:31
  • Compiler asm text output might be useful: relative jumps will always have a target label as an operand. e.g. `jmp .L123`. Some relative jumps (within the same object file) get resolved by the assembler so won't have a relocation entry in the `.o` object file. – Peter Cordes Jun 14 '23 at 05:33
  • There really aren't very many other than jump/call instructions and rip-relative addressing modes. I think searching the disassembly is pretty doable. – prl Jun 14 '23 at 20:27

0 Answers0