-2

What is the use of relocatable machine code which is generated by Assembler? And if generated then whats the use of getting that relocatable machine code to convert it into absolute code?

I watched this video. https://www.youtube.com/watch?v=Qkwj65l_96I&t=309s

In that he mentioned about the Absolute and Relative Machine code

Karsh Soni
  • 170
  • 1
  • 12
  • 1
    So several relocatable machine code object files can be combined into one executable image, and optionally so that executable image can be relocated and loaded a different addresses. – Ross Ridge Sep 04 '17 at 15:06
  • @KarshSoni do you understand what relocatable code means? for the most part relative addresses vs fixed addresses. You can make most if not all of your program use relative addressing which the compiler, assembler, and linker can do the work for you. The point being you can relocate the binary elsewhere. So for example instead of reading at the absolute address 0x1234 from an instruction at address 0x1000 you instead read the location 0x234 bytes ahead of the current address (pc-relative addressing). – old_timer Sep 04 '17 at 15:35
  • https://stackoverflow.com/questions/45945018/does-arm-none-eabi-ld-rewrite-the-bl-instruction/45951427#45951427 is an example using relative addressing and in this case a fixed address table is inserted which has to be modified as part of the relocation. Simple examples can be written that do not require any runtime patching... – old_timer Sep 04 '17 at 15:37
  • ohk thanks @old_timer so the relocatable code uses relative addresses? and if the relocatable is useful as u mentioned. then whats the catch in converting it into Absolute code? please reply i dont have much knowledge about it and i'm trying to learn it so – Karsh Soni Sep 04 '17 at 18:31
  • relative addressing which means program counter based. Why would you need to convert it in the first place, just use it. a lot of times for a lot of instruction sets pc relative addressing is used anyway to save space and/or improve performance. it is when you start to link code and external data items is when relocatable vs not becomes a topic. for relocatable more code is required. Would have to wonder why you would want to undo that once compiled/assembled/linked. just build differently. – old_timer Sep 04 '17 at 22:01
  • you need to pick an architecture (arm, mips, pdp11, msp430, etc good, x86 bad, learn that one last). Examine the instruction set, use feature rich tools like gnu with all the tools and disassemblers, etc. examine how simple function calls are built by the compiler then what the linker does to that with and without -fPIC. As the link I provided above does. I assume everything would become crystal clear after an exercise like that. – old_timer Sep 04 '17 at 22:03
  • @old_timer. I have edited the question please check that out – Karsh Soni Sep 12 '17 at 10:18

1 Answers1

3

so the relocatable code uses relative addresses?

No. At least not necessarily.

Or do you mean "position-independent code" instead of "relocatable code"?

What is the use of relocatable machine code which is generated by Assembler?

Theoretically you could assemble a whole program at once. (Indeed I already did this when I wrote an assembler for a historic CPU.)

However this has one main disadvantage:

Think of the following line of code:

mov [myVariable], eax

Let's say the variable myVariable is located at address 0x1234560. Then in the machine code you'll have the following instruction:

mov [0x1234560], eax

Now you modify one file in your project which consists of ~200 files (which is typical for projects in the automotive industry). Let's say you added some instructions to some file at the start of the project.

This means the addresses of all elements (files) that follow that file in the project will change. Let's say the address of myVariable now is no longer 0x1234560 but 0x1234870.

This means our line of code must now be translated into the following instruction:

mov [0x1234870], eax

Because of this all files in your project must be assembled again!

If you have relocatable code the following instruction is generated:

mov [0], eax

... and some information that the address 0 must be replaced by the address of myVariable.

This means only the addresses must be replaced when one of 200 files changes.

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38