1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”

Slides:



Advertisements
Similar presentations
Programs in Memory Bryce Boe 2012/08/29 CS32, Summer 2012 B.
Advertisements

Program Development Tools The GNU (GNU’s Not Unix) Toolchain The GNU toolchain has played a vital role in the development of the Linux kernel, BSD, and.
Linking & Loading CS-502 Operating Systems
Linking and Loading Fred Prussack CS 518. L&L: Overview Wake-up Questions Terms and Definitions / General Information LoadingLinking –Static vs. Dynamic.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
UEE072HM Embedded Control Systems. Breaking down the compilation process Compilation is made up of a number of phases –Preprocessing –Compilation Can.
Guide To UNIX Using Linux Third Edition
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
F13 Forensic tool analysis Dr. John P. Abraham Professor UTPA.
1 uClinux course. 2 Author: D L Johnson Overview u Day 1 +Survey of embedded operating systems - why uclinux? +The uclinux environment - the directory.
CS 11 C track: lecture 1 Preliminaries Need a CS cluster account cgi-bin/sysadmin/account_request.cgi Need to know UNIX ITS.
Enabling the ARM Learning in INDIA ARM DEVELOPMENT TOOL SETUP.
嵌入式處理器架構與程式設計 王建民 中央研究院 資訊所 2008 年 7 月. 2 Contents Introduction Computer Architecture ARM Architecture Development Tools  GNU Development Tools ARM Instruction.
Compiling & Debugging Quick tutorial. What is gcc? Gcc is the GNU Project C compiler A command-line program Gcc takes C source files as input Outputs.
Lecture 7. Instructions and High-Level to Machine Code Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
Computer Engineering 1 nd Semester Dr. Rabie A. Ramadan 2.
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Old Chapter 10: Programming Tools A Developer’s Candy Store.
Programming With C.
Lecture-1 Compilation process
Linking and Loading Linker collects procedures and links them together object modules into one executable program. Why isn't everything written as just.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Topic 2d High-Level languages and Systems Software
C Tutorial - Program Organization CS Introduction to Operating Systems.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang.
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
CPS120: Introduction to Computer Science Compiling a C++ Program From The Command Line.
Multiple File Compilation and linking By Bhumik Sapara.
First Compilation Rudra Dutta CSC Spring 2007, Section 001.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
C P ROGRAMMING T OOLS. C OMPILING AND R UNNING S INGLE M ODULE P ROGRAM.
1 Linking. 2 Outline What is linking and why linking Complier driver Static linking Symbols & Symbol Table Suggested reading: 7.1~7.5.
CSc 453 Linking and Loading
CS252: Systems Programming Ninghui Li Based on Slides by Gustavo Rodriguez-Rivera Topic 2: Program Structure and Using GDB.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
Hello world !!! ASCII representation of hello.c.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Operating Systems A Biswas, Dept. of Information Technology.
Program Execution and ELF Files Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014 Abed Asi.
The World Leader in High Performance Signal Processing Solutions Toolchain Basics.
Object Files & Linking. Object Sections Compiled code store as object files – Linux : ELF : Extensible Linking Format – Windows : PE : Portable Execution.
Program Execution in Linux David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Programs – Preprocessing, Compilation and Linking
Introduction to C Topics Compilation Using the gcc Compiler
Precept I : Lab Environment, Unix, Bash, Emacs
Slides adapted from Bryant and O’Hallaron
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Linking & Loading.
Computer Engineering 1nd Semester
Program Execution in Linux
Introduction to C Topics Compilation Using the gcc Compiler
CS-3013 Operating Systems C-term 2008
Introduction to C Topics Compilation Using the gcc Compiler
Topic 2e High-Level languages and Systems Software
Linking & Loading CS-502 Operating Systems
Your first C and C++ programs
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Program Execution in Linux
Appendix F C Programming Environment on UNIX Systems
Linking & Loading CS-502 Operating Systems
Introduction to C Topics Compilation Using the gcc Compiler
Debugging.
SPL – PS1 Introduction to C++.
Presentation transcript:

1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”

2 Author: D L Johnson toolchain The compile process cc1 as ld cpp

3 Author: D L Johnson Toolchain – C compiler options u The most simple compile line +gcc myprog.c u Only call the preprocessor (cpp) and c compiler (cc1) +gcc myprog.c -c u Show verbose output on the compile process +gcc -v myprog.c u Produce debugging information for gdb +gcc -g hello.c u Turns on more warnings +gcc -Wall hello.c

4 Author: D L Johnson Toolchain – C compiler options u Optimize the code +gcc –O1 myprog.c … optimise level1 +gcc –O2 myprog.c … optimize level2 +gcc –O3 myprog.c … highest level of optimization +gcc –Os myprog.c … Optimize for size u Add extra include directories +gcc -c hello.c -I/home/djohnson/include u Create assembler code from the c source code +gcc -S hello.c u Do not search the standard system directories for header files only the directories specified with –I +gcc -c –nostdinc -I/home/djohnson/include myprog.c

5 Author: D L Johnson Toolchain – C compiler options u Predrfined macros +gcc -DNO_MM myprog.c u Warn if function is declared or defined without argument type +gcc -Wstrict-prototypes myprog.c u Compiling multiple source files +gcc file1.c file2.c -o myprog u Alternative method for compiling multiple source files +gcc -c file1.c +gcc -c file2.c +gcc file1.o file2.o -o myprog

6 Author: D L Johnson Assignment 2 u Part1: Understand all the gcc options when uClinux compiles a file arm-elf-gcc -D__KERNEL__ - I/home/djohnson/uclinux_project/uClinux /linux-2.4.x/include -Wall -Wstrict- prototypes -Wno-trigraphs -O2 -fno-strict- aliasing -fno-common -fno-common -pipe -fno- builtin -D__linux__ -g -DNO_MM -mapcs-32 - march=armv4 -mtune=arm7tdmi -mshort-load-bytes -msoft-float -nostdinc -iwithprefix include -DKBUILD_BASENAME=filemap -c -o filemap.o filemap.c

7 Author: D L Johnson Toolchain – Linker options u Specifying libraries archives to link in +gcc myprog.c –lmylib –This will search in the default library paths for libraries libmylib.a, libmylib.so u Adding a library path to the list of paths to search +gcc myprog.c –L/home/djohnson/mylibraries u Strip all symbol information from output file +gcc –s myprog.c u Only search library directories specified on common line +gcc –nostdlib myprog.c u First function called when executable loaded +gcc –init mystart myprog.c –Normally linker uses _init as the first function to call

8 Author: D L Johnson Toolchain – binutils - objdump u objdump displays information from object and executable files u Disassemble executable file +objdump –d a.out u Display contents of symbol table +objdump –t a.out u Disassemble from specified start address +objdump –d –start-address=0x

9 Author: D L Johnson Toolchain – binutils - objcopy u Objcopy copies and translates object and executable files into different formats or copies sections out of the file into a new file u Removing sections out of file +Objcopy –O binary –remove-section=.text linux linux.data u Changing the execute address of the binary +Objcopy –O binary –change-section-vma.data=0x linux linux.data u Changing the load address of the binary +Objcopy –O binary –change-section-lma.data=0x linux linux.data

10 Author: D L Johnson ELF file format u ELF = Executable and Linkable format u Originally created by Unix system labs u Used in virtually every recent Unix u Three main types of ELF files +Relocatable file – object file to be linked with other +Executable +Shared object (library u Elf divides the file into sections u A sections is a collection of information of similar type u As seen in the first lecture, for example executable code is placed in.text u Different to eg. MS-DOS binaries where everything is jumbled together

11 Author: D L Johnson ELF file format u Advantage of sections architecture: when executable.text section placed in memory, these locations won’t change u When you ask a kernel to load and run an executable it starts looking in the elf image header for clues on how to load the image u It moves.text into memory and marks read-only u Moves.data into user’s address space as read-write u Finds location and size of.bss section, adds the pages of memory to user’s address space and initialises this memory section to zero u THE uclinux kernel executable is a flat binary not ELF as nothing exists to load it – it must be self existent u Only the files in the romfs file system are elf format as they are loaded by the kernel

12 Author: D L Johnson ELF file format u For hello world > cat hello.c void main(void) { printf(“Hello World”) }; u Type gcc –c hello.c u ELF files contain a table to describe sections within the file u Type readelf –S hello.o

13 Author: D L Johnson ELF file format u The.rel.text section contains the relocations for the.text section of the file u Notice printf needs to be relocated

14 Author: D L Johnson ELF file format u This is done through the PLT (Procedure Link Table)

15 Author: D L Johnson ELF file format u Compare the assembler code of the hello.o object file gcc –c hello.c objdump –d hello.o u To this assembler code of the a.out executable gcc hello.c objdump –d a.out u Notice all the extra assembler in a.out such as the.init section, the.plt section which points to the real address of printf

16 Author: D L Johnson ELF file format u More info on the ELF file format can be found in documents in day3 folder +Elf format presentation.pdf +Elf_format.pdf

17 Author: D L Johnson Assignment 3 u Take a standard hello.c file int main(void) { char name[10] = “albert”; printf(“my name is %s”, name); return 0 } u Change “albert” to “david” using assembler (change the hello.s file) u Create a new executable +Hint useful tools –gcc will compile c code files (myfile.c) and assember files (myfile.s) u If you’re bored do this with arm environment –Hint – try changing the Makefile in user/hello to create assembler code