Memory Layout and Linking Process in Programming

memory layout for process n.w
1 / 8
Embed
Share

Dive into the intricate world of memory layout and the linking process in programming, exploring topics such as creating process files, object files, executables, and the role of linkers. Follow a simple example to grasp the concepts better.

  • Programming
  • Memory Layout
  • Linking Process
  • Object Files
  • Executables

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Memory Layout for Process Stack Data Code 0 CS 111 Lecture Notes: Linkers Slide 1

  2. Creating a Process Source Code Assembly Code Object File Executable 10101010 10101010 10101010 10101010 10101010 10101010 x.o x.c gcc x.s as Stack 10101010 10101010 10101010 10101010 10101010 10101010 y.o 10101010 10101010 10101010 10101010 10101010 10101010 a.out OS Data y.c gcc y.s as ld Code 0 10101010 10101010 10101010 10101010 10101010 10101010 z.o z.c gcc z.s as Compiler Assembler Linker Loader CS 111 Lecture Notes: Linkers Slide 2

  3. A Simple Example main.c stdio.c extern float sin(); extern printf(), scanf(); FILE* stdin, stdout; int printf(const char* format,...) { ... fputc(c, stdout); ... } main() { double x, result; printf("Type number: "); scanf("%f", &x); result = sin(x); printf("Sine is %f\n", result); } int scanf(const char* format,...) { ... c = fgetc(stdin); ... } math.c double sin(double x) { ... } CS 111 Lecture Notes: Linkers Slide 3

  4. main.o Object File main.c main.o main: ... call printf ... call scanf ... call sin ... call printf text section extern float sin(); extern printf(), scanf(); 0 30 main() { double x, result; printf("Type number: "); scanf("%f", &x); result = sin(x); printf("Sine is %f\n", result); } 52 60 86 data section _s1: "Type number: " _s2: "%f" _s3: "Sine is %f\n" 0 14 17 symbols main T[0] _s1 D[0] _s2 D[14] _s3 D[17] Store the final location of sin at offset 60 in the text section unresolved references printf T[30] printf T[86] scanf T[52] sin T[60] _s1 T[24] _s2 T[54] _s3 T[80] CS 111 Lecture Notes: Linkers Slide 4

  5. stdio.o Object File stdio.c FILE* stdin, stdout; stdio.o ... printf: ... load stdout ... scanf: ... load stdin ... text section 44 int printf(const char* format, ...) { ... fputc(c, stdout); ... } 118 232 306 int scanf(const char* format, ...) { ... c = fgetc(stdin); ... } data section stdin: stdout: 0 8 symbols printf T[44] scanf T[232] stdin D[0] stdout D[8] unresolved references stdout T[118] fputc T[122] stdin T[306] fgetc T[310] CS 111 Lecture Notes: Linkers Slide 5

  6. After Pass 1 Memory map: 836 stdio.o data 760 720 main.o data math.o text 508 stdio.o text 96 main.o text 0 CS 111 Lecture Notes: Linkers Slide 6

  7. After Pass 2 Memory map: Symbol table: 836 Name main _s1 _s2 _s3 printf scanf stdin stdout sin File main.o main.o main.o main.o stdio.o stdio.o stdio.o stdio.o math.o Sec T D D D T T D D T Offset 0 0 14 17 38 232 0 8 0 Addr 0 720 734 737 134 328 760 768 508 stdio.o data 760 720 main.o data math.o text 508 stdio.o text 96 main.o text 0 CS 111 Lecture Notes: Linkers Slide 7

  8. Resolving References ... call 0 ... text section in main.o 30 unresolved reference in main.o printf T[30] symbol table printf: 134 ... text section in a.out call 134 ... 30 CS 111 Lecture Notes: Linkers Slide 8

More Related Content