Calling routine's responsibilities
When an assembler language program calls another program, the caller should not use the names of the called program's commands, functions, or procedures as global assembler language symbols. To avoid confusion, follow the naming conventions for the language of the called program when you create symbol names. For example, if you are calling a C language program, be certain you use the naming conventions for that language.
A called routine has two symbols associated with it: a function descriptor (Name) and an entry point (.Name). When a call is made to a routine, the compiler branches to the entry point directly.
Calls to functions are expanded by compilers to include an NOP instruction after each branch and link instruction. If necessary, this extra instruction is modified by the linkage editor to restore the contents of the TOC register (register 2) on return from an out-of-module call.
bl .foo #Branch to foo
nop The linkage editor will do one of two things when it sees the bl instruction (in the previous instruction sequence, on a call to the foo function):
- If the foo function is imported (not in the same executable module), the linkage editor:
- Changes the bl .foo instruction to bl .glink_of_foo (a global linkage routine).
- Inserts the .glink code sequence into the (/usr/lib/glink.o file) module.
- Replaces the NOP instruction with an lwz (load) instruction to restore the TOC register.
bl .glink_of_foo #Branch to global linkage routine for foo lwz 2,20(1) #Restore TOC register instruction 0x80410014 - If the foo function is subsequently bound in the same executable module as its caller, the linkage editor:
- Changes the bl .glink_of_foo sequence (a global linkage routine) to bl .foo.
- Replaces the restore TOC register instruction with a NOP instruction.
The bl .glink_of_foo instruction sequence is changed to:bl .foo #Branch to foo nop