[Dwarf-Discuss] Stack tracing and return addresses off by 1

Tye, Tony Tony.Tye@amd.com
Fri Jul 31 22:30:42 GMT 2020


To add to what Greg described you may also want to look at section 6.4.4 of the DWARF 5 specification that discusses these issues. In particular note the non-normative description in the fourth paragraph.

> 6.4.4 Call Frame Calling Address
> 
> *When virtually unwinding frames, consumers frequently wish to obtain the address of the instruction which called a subroutine. This information is not always provided. Typically, however, one of the registers in the virtual unwind table is the Return Address.*
> 
> If a Return Address register is defined in the virtual unwind table, and its rule is  undefined (for example, by DW_CFA_undefined), then there is no return address and no call address, and the virtual unwind of stack activations is complete.
> 
> *In most cases the return address is in the same context as the calling address, but that need not be the case, especially if the producer knows in some way the call never will return. The context of the ?return address? might be on a different line, in a different lexical block, or past the end of the calling subroutine. If a consumer were to assume that it was in the same context as the calling address, the virtual unwind might fail.*
> 
> *For architectures with constant-length instructions where the return address immediately follows the call instruction, a simple solution is to subtract the length of an instruction from the return address to obtain the calling instruction. For architectures with variable-length instructions (for example, x86), this is not possible. However,  subtracting 1 from the return address, although not guaranteed to provide the exact calling address, generally will produce an address within the same context as the calling address, and that usually is sufficient.*

Thanks,
-Tony Tye



More information about the Dwarf-discuss mailing list