unix shell - revisited

17
Unix Shell - Revisited Runs programs – command line Not the only program that runs other programs make gcc and more … How this happens ? Who is responsible for that? Operating System System call exec family - execvp, execl, execv, … 2

Upload: kaiser

Post on 23-Feb-2016

78 views

Category:

Documents


0 download

DESCRIPTION

Unix Shell - Revisited. Runs programs – command line Not the only program that runs other programs m ake gcc and more … How this happens ? Who is responsible for that? Operating System System call exec family - execvp , execl , execv , … . Unix Shell - Revisited. These functions - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Unix Shell - Revisited

2

Unix Shell - Revisited Runs programs – command line

Not the only program that runs other programs make gcc and more …

How this happens ? Who is responsible for that? Operating System System call exec family - execvp, execl, execv, …

Page 2: Unix Shell - Revisited

3

Unix Shell - Revisited These functions

receive the path of the program executable file receive a list of the program arguments loads the program into memory, and run it …

Last lecture, fork( ) and exec( ) why these function are used together ?

Page 3: Unix Shell - Revisited

4

Executable Files Unix has several different types of executable files

You are already familiar with some of them: Binary files Bash script more ?

Are these files executed similarly by the OS ?

Page 4: Unix Shell - Revisited

5

Executable Files – (cont’d)

Binary file Compiled – intermediate binary Loading data and code to memory Moving control to the entry point Running the machine-code

Scripts No compilation We don’t have the binary code nor the memory content A specified program parses and runs the commands Commands are interpreted line-by-line

How do we specify which interpreter runs the script ?

Page 5: Unix Shell - Revisited

6

Executable Files – (cont’d)

The first line - #!/bin/sh

exec( ) uses this to determine the type of the executable

shebang is encountered it is a script file

The first line contains the path of the relevant program too What can we do with it ?

Page 6: Unix Shell - Revisited

7

Executable Files – (cont’d)

No sanity checks for the shebang line

Every program/utility could appear there

#!/bin/cat

  Hello world

#!/bin/rm  

ls -l

Page 7: Unix Shell - Revisited

8

ELF – Executable and Linkable Format

Page 8: Unix Shell - Revisited

9

ELF – Executable and Linkable Format

ELF defines a format of executable binary files

There are three main types

Relocatable▪ Created by compilers or assemblers. Need to be processed by the linker before

running

Executable▪ Have all relocation done and all symbol resolved except perhaps shared library

symbols that must be resolved at run time

Shared Object▪ Shared library containing both symbol information for the linker and directly

runnable code for run time

Page 9: Unix Shell - Revisited

10

ELF – Dual Nature Compilers, assemblers, and linkers treat the file as a set of

logical sections

The system loader treats the file as a set of segments

Sections are intended for further processing by a linker

Segments are intended to be mapped into memory

Page 10: Unix Shell - Revisited

ELF StructureProgram and section header table offsets

Page 11: Unix Shell - Revisited

12

Your Best Examplesection .text

global _start ;  

_start: ;tell linker entry point  

mov edx,len ;message length mov ecx,msg ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel

  mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel

section .data  

msg db 'Hello world!',0xa ;our dear string len equ $ - msg ;length of our dear string

Page 12: Unix Shell - Revisited

13

ELF Header - Hello Example

Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00Class: ELF32Data: 2's complement, little endianVersion: 1 (current)OS/ABI: UNIX - System VABI Version: 0Type: EXEC (Executable file)Machine: Intel 80386Version: 0x1Entry point address: 0x8048080Start of program headers: 52 (bytes into file)Start of section headers: 256 (bytes into file)Flags: 0x0Size of this header: 52 (bytes)Size of program headers: 32 (bytes)Number of program headers: 2Size of section headers: 40 (bytes)Number of section headers: 7Section header string table index: 4

Page 13: Unix Shell - Revisited

14

Relocatable Files A relocatable or shared object file is a collection of sections

Each section contains a single type of information, such as program code, read-only data, read/write data, symbols

Every symbol’s address is defined relative to a section For example, the entry point is relative to the code section

Page 14: Unix Shell - Revisited

15

ELF Sections – Hello Example

[Nr] Name Type Addr Off Size [ 0] NULL 00000000 000000 000000 000000 [ 1] .text PROGBITS 08048080 000080 00001d[ 2] .data PROGBITS 080490a0 0000a0 00000e[ 3] .comment PROGBITS 00000000 0000ae 00001f [ 4] .shstrtab STRTAB 00000000 0000cd 000030[ 5] .symtab SYMTAB 00000000 000218 0000b0[ 6] .strtab STRTAB 00000000 0002c8 000032

Page 15: Unix Shell - Revisited

16

Special Section - Symbol Table

An object file symbol table holds information needed to locate and relocate a program’s symbolic definition and references

Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 1: 00000000 0 FILE LOCAL DEFAULT ABS hello.asm 2: 00000000 0 SECTION LOCAL DEFAULT 1 3: 00000000 0 SECTION LOCAL DEFAULT 2 4: 00000000 0 NOTYPE LOCAL DEFAULT 2 msg 5: 0000000e 0 NOTYPE LOCAL DEFAULT ABS len 6: 0000000e 0 NOTYPE LOCAL DEFAULT ABS len 7: 08048080 0 NOTYPE GLOBAL DEFAULT 1 _start 8: 080490ae 0 NOTYPE GLOBAL DEFAULT ABS __bss_start 9: 080490ae 0 NOTYPE GLOBAL DEFAULT ABS _edata 10: 080490b0 0 NOTYPE GLOBAL DEFAULT ABS _end

Page 16: Unix Shell - Revisited

17

Executable Files

Page 17: Unix Shell - Revisited

18

Navigate through the Maze