Open In App

Introduction of Object Code in Compiler Design

Improve
Improve
Like Article
Like
Save
Share
Report

Let assume that, you have a c program, then you give the C program to compiler and compiler will produce the output in assembly code. Now, that assembly language code will give to the assembler and assembler is going to produce you some code. That is known as Object Code

In the context of compiler design, object code is the intermediate code that is generated by the compiler after the syntax analysis, semantic analysis, and optimization stages. Object code is essentially the machine-readable version of the source code, which can be executed directly by the computer’s CPU.

  1. Object code is typically stored in a binary file format, which is specific to the target architecture and operating system. The object code file contains both the executable code and data, as well as information about the program’s symbols and their memory locations.
  2. Object code is generated by the compiler in multiple steps. First, the source code is transformed into an intermediate representation, such as an abstract syntax tree or a three-address code. Then, the intermediate code is optimized to improve the efficiency and speed of the final executable code. Finally, the optimized intermediate code is translated into the target architecture’s machine code, using the appropriate instruction set and addressing modes.
  3. Object code can be linked with other object files to produce a complete executable program. The linking process involves resolving any unresolved external references, such as function calls or global variables, and generating a final executable file that can be run on the target system.

In summary, object code is the machine-readable code that is generated by the compiler, and it serves as an intermediate step between the source code and the final executable code. Object code files are specific to the target architecture and operating system and are typically stored in a binary file format.

compilation But, when you compile a program, then you are not going to use both compiler and assembler. You just take the program and give it to the compiler and compiler will give you the directly executable code. The compiler is actually combined inside the assembler along with loader and linker.So all the module kept together in the compiler software itself. So when you calling gcc, you are actually not just calling the compiler, you are calling the compiler, then assembler, then linker and loader. Once you call the compiler, then your object code is going to present in Hard-disk. This object code contains various part – compilation2

  • Header – The header will say what are the various parts present in this object code and then point that parts. So header will say where the text segment is going to start and a pointer to it and where the data segment going to start and it say where the relocation information and symbol information there. It is nothing but like an index, like you have a textbook, there an index page will contain at what page number each topic present. Similarly, the header will tell you, what are the palaces at which each information is present. So that later for other software it will be useful to directly go into those segment.
  • Text segment – It is nothing but the set of instruction.
  • Data segment – Data segment will contain whatever data you have used. For example, you might have used something constraint, then that going to be present in the data segment.
  • Relocation Information – Whenever you try to write a program, we generally use symbol to specify anything. Let us assume you have instruction 1, instruction 2, instruction 3, instruction 4,…. compilation3 Now if you say somewhere Goto L4 (Even if you don’t write Goto statement in the high-level language, the output of the compiler will write it), then that code will be converted into object code and L4 will be replaced by Goto 4. Now Goto 4 for the level L4 is going to work fine, as long as the program is going to be loaded starting at address no 0. But in most cases, the initial part of the RAM is going to be dedicated to the operating system. Even if it is not dedicated to the operating system, then might be some other process that will already be running at address no 0. So, when you are going to load the program into memory, means if the program has to be loaded in the main memory, it might be loaded anywhere.Let us say 1000 is the new starting address, then all the addresses have to be changed, that is known as Reallocation. compilation4 The original address is known as Relocatable address and the final address which we get after loading the program into main memory is known as the Absolute address.

Symbol table –

  • It contains every symbol that you have in your program. for example, int a, b, c then, a, b, c are the symbol.it will show what are the variables that your program contains.

 

Features :

Machine-readable format: Object code is in a format that can be executed directly by the processor without the need for further translation.

Architecture-specific: Object code is specific to a particular processor architecture, so it must be recompiled for other architectures.

Linking: Object code can be linked together with other object files and libraries to create a complete executable program.

Debugging information: Object code can include debugging information, such as line numbers and variable names, to aid in debugging the program.

Relocation information: Object code includes information about the addresses of symbols in the code, allowing the linker to adjust the addresses when the code is linked with other code.

Code optimization: Object code can be optimized by the compiler to improve performance, reduce code size, or both.

Assembly code: Object code can be disassembled into assembly code, which can be useful for understanding how the program works or for reverse engineering.

Advantages:

  1. Efficiency: Object code is optimized for the specific target platform, which can result in more efficient code than would be possible with a high-level language.
  2. Portability: Object code is typically platform-specific, but it can still be portable across different systems that use the same platform. This allows developers to write code once and compile it for multiple target systems.
  3. Debugging: Object code can be easier to debug than source code, as it provides a low-level view of the program’s execution. Developers can use object code to trace the execution of the program and identify errors or issues that may be present.
  4. Protection: Object code can be protected through the use of obfuscation techniques, making it harder for others to reverse engineer the code or steal intellectual property.
  5. Security: Object code is more secure than source code because it is not readable by humans, making it more difficult for attackers to reverse engineer the code.
  6. Interoperability: Object code can be easily linked with other object files to create a complete executable program.

Disadvantages:

  1. Platform-specific: Object code is specific to a particular platform, which means that it may not be compatible with other systems. This can limit the portability of the code and make it harder to deploy across multiple systems.
  2. Limited readability: Object code is a low-level language that is harder to read and understand than source code. This can make it more difficult for developers to maintain and debug the code.
  3. Limited control: Object code is generated by the compiler, and developers have limited control over the resulting code. This can limit the ability to optimize the code or tailor it to specific requirements.
  4. Compatibility issues: Object code can sometimes be incompatible with other components of the system, which can cause errors or performance issues.
  5. Code size: Object code is typically larger than source code because it contains additional information, such as symbols and relocation information.
  6. Licensing: Object code may be subject to licensing restrictions that limit its use and distribution.

Debugging information –

  • It will help to find how a variable keeps on changing. GATE CS Corner Questions Practicing the following questions will help you test your knowledge. All questions have been asked in GATE in previous years or in GATE Mock Tests. It is highly recommended that you practice them. 1. GATE-CS-2001 | Question 17

Last Updated : 12 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads