gpio timer uart printf malloc keyboard fb gl console shell

Similar documents
Link Edits and Relocatable Code

Linking. Explain what ELF format is. Explain what an executable is and how it got that way. With huge thanks to Steve Chong for his notes from CS61.

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

C compiler. Memory map. Program in RAM

Linking and Loading. ICS312 - Spring 2010 Machine-Level and Systems Programming. Henri Casanova

Running C programs bare metal on ARM using the GNU toolchain

CSE 2421: Systems I Low-Level Programming and Computer Organization. Linking. Presentation N. Introduction to Linkers

Executables and Linking. CS449 Spring 2016

(Extract from the slides by Terrance E. Boult

Compiler Drivers = GCC

CS2141 Software Development using C/C++ Libraries

M2 Instruction Set Architecture

Generating Programs and Linking. Professor Rick Han Department of Computer Science University of Colorado at Boulder

Today s Big Adventure

Today s Big Adventure

Lecture 3: Instruction Set Architecture

CSC 1600 Memory Layout for Unix Processes"

2 Compiling a C program

Advanced Operating Systems Embedded from Scratch: System boot and hardware access. Federico Terraneo

Executables and Linking. CS449 Fall 2017

button.c The little button that wouldn t

Link 3. Symbols. Young W. Lim Mon. Young W. Lim Link 3. Symbols Mon 1 / 42

COMPILING OBJECTS AND OTHER LANGUAGE IMPLEMENTATION ISSUES. Credit: Mostly Bryant & O Hallaron

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

238P: Operating Systems. Lecture 7: Basic Architecture of a Program. Anton Burtsev January, 2018

Exercise Session 7 Computer Architecture and Systems Programming

CS 107 Lecture 18: GCC and Make

Computer Organization & Assembly Language Programming (CSE 2312)

Keyboards. The PS/2 Protocol

ECE260: Fundamentals of Computer Engineering

Embedded Systems Programming

Computer Systems Organization

Makefiles SE 2XA3. Term I, 2018/19

ECE 471 Embedded Systems Lecture 6

ECE 598 Advanced Operating Systems Lecture 10

Systems Programming. Fatih Kesgin &Yusuf Yaslan Istanbul Technical University Computer Engineering Department 18/10/2005

Important Upgrade Information. iii P a g e

CS 550 Operating Systems Spring Process I

Lecture 03 Bits, Bytes and Data Types

Lecture 8: linking CS 140. Dawson Engler Stanford CS department

Optimizing C For Microcontrollers

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Important Upgrade Information

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Embedded Software TI2726 B. 3. C tools. Koen Langendoen. Embedded Software Group

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Introduction to Supercomputing

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

CSE 509: Computer Security

Binghamton University. CS-220 Spring Loading Code. Computer Systems Chapter 7.5, 7.8, 7.9

High Performance Computing Lecture 1. Matthew Jacob Indian Institute of Science

PROGRAM COMPILATION MAKEFILES. Problem Solving with Computers-I

ECE 498 Linux Assembly Language Lecture 1

Lecture 10: Program Development versus Execution Environment

ARM Assembly Programming

ECE 471 Embedded Systems Lecture 5

L2 - C language for Embedded MCUs

NET3001. Advanced Assembly

Outline. Unresolved references

Memory and C/C++ modules

ARM Assembly Programming

ECE 598 Advanced Operating Systems Lecture 11

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

Building an EZ-Host/OTG Project From Start to Finish

Reviewing gcc, make, gdb, and Linux Editors 1

o Code, executable, and process o Main memory vs. virtual memory

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

CSE 333 Midterm Exam 5/9/14 Sample Solution

Computer Organization & Assembly Language Programming (CSE 2312)

CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduction to C, Part I

Raspberry Pi / ARM Assembly. OrgArch / Fall 2018

ECE 3210 Laboratory 1: Develop an Assembly Program

ARM Assembly Programming II

Intermediate Programming, Spring 2017*

ECE 471 Embedded Systems Lecture 4

CSE 333 Lecture 2 Memory

A Simplistic Program Translation Scheme

TMS470 ARM ABI Migration

Programming Assignment 1 (PA1) - Display Bowtie

CNIT 127: Exploit Development. Ch 3: Shellcode. Updated

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

CS 361 Computer Systems Fall 2017 Homework Assignment 1 Linking - From Source Code to Executable Binary

EE458 - Embedded Systems Lecture 4 Embedded Devel.

Example C program. 11: Linking. Why linkers? Modularity! Static linking. Why linkers? Efficiency! What do linkers do? 10/28/2013

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

EECS 373 Design of Microprocessor-Based Systems

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

From Source to Execution:

CS 161 Computer Security

Please refer to the turn-in procedure document on the website for instructions on the turn-in procedure.

Segmentation in Assembly Language Programming

Chapter 11 Introduction to Programming in C

VORAGO VA108x0 GCC IDE application note

Department of Computer Science and Engineering Yonghong Yan

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

Low-Level Essentials for Understanding Security Problems Aurélien Francillon

Midterm. Median: 56, Mean: "midterm.data" using 1:2 1 / 37

Chapter 11 Introduction to Programming in C

Transcription:

Where are We Going? Processor and memory architecture Peripherals: GPIO, timers, UART Assembly language and machine code From C to assembly language Function calls and stack frames Serial communication and strings Modules and libraries: Building and linking Memory management: Memory map & heap

gpio timer uart printf malloc keyboard fb gl console shell

Good Modules Decompose a system into smaller parts (modules) Interface: what the module does Implementation: how it does it A good interface An easy-to-understand abstraction that simplifies code Can be implemented easily and in different ways Tested independently with unit tests

Example: printf int printf(const char* format, ); printf() does a lot of things Parses a format string Converts arguments into strings Concatenates format string and arguments into a longer string Outputs string to terminal/serial port

Example: printf int printf(const char* format, ); printf() does a lot of things 1. Outputs string to terminal/serial port 2. Concatenates format string and arguments into a longer string 3. Parses a format string 4. Converts arguments into strings An example decomposition, recommended in the assignment, that breaks the problem into smaller, simpler parts.

Example: printf int printf(const char* format, ); printf() does a lot of things 1. Outputs string to terminal/serial printf() port 2. Concatenates format string and arguments into a longer string snprintf() 3. Parses a format string 4. Converts arguments sign_to_base() into strings An example decomposition, recommended in the assignment, that breaks the problem into smaller, simpler parts.

Linking Your program uses multiple modules, each with its own abstraction and logic Some modules call other modules: you need module A to be able to invoke a function in module B Your tools link them together, somehow combining them into a single block of binary code

"The Build"

gcc is all powerful gpio.c gpio.i gpio.s gpio.o cpp cc1 as gcc -E gcc -S gcc -c gcc save-temps

main.c clock.c main.o clock.o Linking gpio.c timer.c gpio.o timer.o main.elf cstart. start.s cstart.o start.o ld (gcc)

Common Errors 1. Symbol undefined 2. Symbol multiply defined

Symbols Single global name space need gpio_ prefix to distinguish names e.g. gpio_init versus timer_init Local variables in functions are not symbols Defined vs. undefined (extern) symbols Definitions: global vs local (static) by default symbols are local in.s files by default symbols are global in.c files

Question C doesn t support polymorphic functions. You can t have both itoa(char *buf, int bufsize, short val); itoa(char *buf, int bufsize, int val); There can be only one symbol itoa in object files C++ does support polymorphic functions ostream& operator<< (short val); ostream& operator<< (unsigned short val); How does this work?

Symbol Resolution Set of defined symbols D Set of undefined symbols U Moving left to right, for each.o file, the linker updates U and D and proceeds to next input file. Problems If two files try to define the same symbol, an error is reported*** After all the input arguments are processed, if U is found to be not empty, the linker prints an error report and terminates.

Libraries An archive.a is just a collection of.o files. The linker scans the library for any.o files that contain definitions of undefined symbols. If a file in the library contains an undefined symbol, the whole file and all its functions are linked in. Adding the.o file from the library may result in more undefined symbols; the linker searches for the definition of these symbols in the library and includes the relevant files; this search is repeated until no more definitions of undefined symbols are found.

Questions? Suppose you add more functions to the clock interface (e.g. clock_start(), clock_tick(), clock_end()) what source files would need to be modified? rebuilt? Can you think of a way to force linking if you change OBJECTS in a Makefile? What happens if you link with ld -lpi gpio.o?

When to Rebuild? Change to implementation (clock.c)? Must recompile implementation (clock.o) Change to interface (clock.h)? Should (must) recompile clients of the interface (main.c) Add recipe that main.o depends on clock.h Change to Makefile Adding a file to OBJECTS may require rebuilding executable main.elf Modify recipe for main.elf to depend on Makefile BEWARE: This is typical of a hidden dependency

Combining Multiple Modules (.o) into a Single Executable (.elf) memmap

// memmap MEMORY { ram : ORIGIN = 0x8000, LENGTH = 0x8000000 }.text : { start.o (.text) *(.text*) } > ram // Why must start.o go first?

_start must be located at #0x8000!! Magic constant that s part of Raspberry Pi boot sequence.

% arm-none-eabi-nm -n main.elf 00008000 T _start 00008008 t hang 0000800c T main 00008020 T timer_init 00008024 T timer_get_time 0000802c T delay_us 00008038 T delay 0000806c T gpio_init 00008070 T gpio_set_function 00008074 T gpio_get_function 00008078 T gpio_write 0000807c T gpio_read 00008080 T clock_init 00008084 T clock_run 00008088 T _cstart 000080d8 T bss_end 000080d8 T bss_start

# size reports the size of the text % arm-none-eabi-size main.elf text data bss dec hex filename 216 0 0 216 d8 main.elf % arm-none-eabi-size *.o text data bss dec hex filename 8 0 0 8 8 clock.o 80 0 0 80 50 cstart.o 20 0 0 20 14 gpio.o 20 0 0 20 14 main.o 12 0 0 12 c start.o 76 0 0 76 4c timer.o # Note that the sum of the sizes of the.o's # is equal to the size of the main.exe

Relocation

// start.s.globl _start start: mov sp, #0x8000000 mov fp, #0 bl _cstart hang: b hang

// Disassembly of start.o (start.list) 0000000 <_start>: 0: mov sp, #0x8000000 4: mov fp, #0 8: bl 0 <_cstart> 0000000c <hang>: c: b c <hang> // Note: the address of _cstart is 0 // Why? // _start doesn't know where c_start is! // Note it does know the address of hang

// Disassembly of main.elf.list 00008000 <_start>: 8000: mov sp, #134217728 ; 0x8000000 8004: bl 8088 <_cstart> 00008008 <hang>: 8008: b 8008 <hang> 00008088 <_cstart>: 8088: push {r3, lr} // Note: the address of _cstart is #8088 // Now _start knows where _cstart is!

data/

// uninitialized global and static int i; static int j; // initialized global and static int k = 1; static int l = 2; // initialized global and static const const int m = 3; static const int n = 4;

% arm-none-eabi-nm -S tricky.o 00000004 00000004 C i 00000000 00000004 b j 00000000 00000004 D k 00000004 00000004 d l 00000000 00000004 R m U p 00000000 00000048 T tricky # The global uninitialized variable i # is in common (C). # The static const variable n # has been optimized out.

Guide to Symbols T/t - text D/d - read-write data R/r - read-only data B/b - bss (Block Started by Symbol) C - common (instead of B) lower-case letter means static

Data Symbols Types global vs static read-only data vs data initialized vs uninitialized data common (shared data)

Sections Instructions go in.text Data goes in.data const data (read-only) goes in.rodata Uninitialized data goes in.bss + other information about the program symbols, relocation, debugging,

.text : { start.o (.text) *(.text*) } > ram.data : { *(.data*) } > ram.rodata : { *(.rodata*) } > ram bss_start =.;.bss : { *(.bss*) *(COMMON) } > ram. = ALIGN(8); bss_end =.;

MEMORY { ram : ORIGIN = 0x8000, LENGTH = 0x8000000 }.text : { start.o (.text) *(.text*) } > ram.data : { *(.data*) } > ram.rodata : { *(.rodata*) } > ram bss_start =.;.bss : { *(.bss*) *(COMMON) } > ram. = ALIGN(8); bss_end =.; bss_end bss_start 0x8000 COMMON of all modules.bss of all modules.rodata of all modules.data of all modules.text of other modules.text of start.o

Builds Automate the build! Manual builds are error prone Needs to be fast and reliable Fast means compile modules only when necessary Reliably means keeping track of dependencies between files Separate system into small modules with minimal dependencies Ensure Makefile contains all dependencies

Summary Decomposing software into modules is a critical skill Like organizing an essay into sections, paragraphs, sentences The linker combines modules into a larger binary Resolves undefined symbols to modules that define them Lays out data and code Relocation when needed Makefiles designed to only compile modules that need recompilation If F changes, recompile everything that depends on F