Run-time C program

Source: Internet
Author: User

Data and Code

One of the classical opposites of programming language theory is the distinction between code and data, and some languages such as Lisp regard both as a whole, while other languages such as C maintain the difference. Compiling most of the work is related to translating code, and the necessary data storage management is never part of the run-time.

Learning to run can have three benefits, help optimize code, get the best efficiency, help to understand more advanced materials, and get into trouble when it's easier to analyze problems.

Segment (segments)

ELF: (originally EXTENSIBLA Linker format, extensible linker formats, now representing executable and linking format, executables and link formats), exists in most SVR4 implementations.
COFF: (Common ojbect-file format, normal destination file formats), exists in other systems.

The two different formats have a common concept, the concept of paragraph, paragraph and many are related, in the case of the target file, the paragraph refers to the simple area of the binary file, which holds information related to a particular type (such as symbol table entries). The term section is also widely used, which is the smallest organizational unit in an elf file, and a segment typically contains several sections.

The concept of the above paragraph is in UNIX and has nothing to do with the segments of the Intel X86 architecture. The middle of Unix represents a binary file content block, Intel X86 middle section represents a design result, in this design, the address space is not a whole, but is divided into 64k size area, called paragraph, this design is also the love must have, in order to backwards compatible with the chip.

Structure of the A.out

Shows what the compiler and connector wrote in each of these segments

The BSS segment is the abbreviation for block Started by symbol (blocks starting with symbols), which is a pseudo-directive of the old IBM704 assembler, and Unix borrows the name, which is still in use today. Some people like to remember it as "Better save Space", because the BSS segment only holds no worthwhile variables, so it's not really necessary to save the mappings for these variables. The size of the BSS segment required by the runtime is recorded in the target file, but the BSS segment is not like other segments, and it does not occupy any space in the destination file

You can use the size command and the NM program to see the sizes of each part of the program

Operating system for A.out

Why use a field to organize, because the segment can be easily mapped to the linker, at run time can be loaded directly, the loader only need to extract the image of each segment in the file. A segment is an area of memory in an executing program, and each region has a specific purpose.


Text segment includes instructions for the program
The data segment includes the initialized global and static variables and their values, the size of the BSS is obtained from the executable file, and then the block of memory is linked to that size, and immediately after the block, the memory is zeroed out after it enters the program.

C Language runtime system for A.out

Runtime data structures include, stack, activity record, data and heap, etc.

    • Stack segment: There are three main uses, related to functions and expressions. In addition to recursive calls, the stack is not required. In most CPUs, the stack grows downward, that is, toward the low address direction.
      • Provides storage space for local variables inside a function
      • During a function call, maintenance information is maintained, called a stack structure or process activity record
      • Staging area, a long arithmetic expression.

Unlike C + +, C run-time functions are short and concise, making C very efficient
The C language does not allow functions to be defined in functions, that is, functions cannot be nested definitions.

SETJMP and longjmp

The above two commands are unique to the C language and are implemented by manipulating the process activity records.

    • setjmp (Jmp_buf j) must be called first. It indicates that the current position is recorded using the variable J, and the function returns 0;
    • longjmp (jmp_buf j,int i) can then be called, which means returning to the position recorded by J so that he looks as if he had just returned from the setjmp function. But the function returns I so that the code knows it is actually returned by LONGJMP.
    • When using longjmp, the contents of J are destroyed
      • setjmp saves a copy of the program counter and the current stack-top pointer, or it can save some initial values, longjmp restore the initial values.
      • Unlike Goto, first, Goto cannot jump out of the C-language function; second, longjmp can only go back to where it once was.
    • It is important to note that the only reliable way to ensure that a local variable has been maintained during the LONGJMP process is to declare it as volatile.
    • The maximum use of the combination is error recovery, as long as the function is not returned from the functions, once an unrecoverable error is found, you can transfer the control flow to the main input loop. It can be used to return immediately from a string of very deep code, to beware of potentially dangerous code. The exception system is currently supported by C + +
switch(setjmp(jbuf)){    case0:          apple = *suspicious;          break;    case1:          printf("suspicious is a bad pointer\n");          break;    default:          die("unexpected value returned bt setjmp");}
    • As with Goto, do not use them when necessary.
UNIX and MS-DOS stack segments
    • In Unix, the stack will grow automatically when the process needs more space.
    • DOS, when an executable is created, the stack size must be determined at the same time, and it cannot grow at run time. Stack overflow is a common heap overflow error.
Useful C-language tools


Reference

C Expert Programming

Run-time C program

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.