Sta C Linking

Total Page:16

File Type:pdf, Size:1020Kb

Sta C Linking Carnegie Mellon Linking 15-213: Introduc;on to Computer Systems 13th Lecture, Oct. 13, 2015 Instructors: Randal E. Bryant and David R. O’Hallaron Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 1 Carnegie Mellon Today ¢ Linking ¢ Case study: Library interposioning Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 2 Carnegie Mellon Example C Program int sum(int *a, int n);! int sum(int *a, int n)! ! {! int array[2] = {1, 2};! int i, s = 0;! ! ! int main()! for (i = 0; i < n; i++) {! {! s += a[i];! int val = sum(array, 2);! }! return val;! return s;! }! }! main.c ! sum.c Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 3 Carnegie Mellon Sta7c Linking ¢ Programs are translated and linked using a compiler driver: § linux> gcc -Og -o prog main.c sum.c § linux> ./prog main.c sum.c Source files Translators Translators (cpp, cc1, as) (cpp, cc1, as) main.o sum.o Separately compiled relocatable object files Linker (ld) Fully linked executable object file prog (contains code and data for all func;ons defined in main.c and sum.c) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 4 Carnegie Mellon Why Linkers? ¢ Reason 1: Modularity § Program can be wriNen as a collec;on of smaller source files, rather than one monolithic mass. § Can build libraries of common func;ons (more on this later) § e.g., Math library, standard C library Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 5 Carnegie Mellon Why Linkers? (cont) ¢ Reason 2: Efficiency § Time: Separate compilaon § Change one source file, compile, and then relink. § No need to recompile other source files. § Space: Libraries § Common func;ons can be aggregated into a single file... § Yet executable files and running memory images contain only code for the func;ons they actually use. Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 6 Carnegie Mellon What Do Linkers Do? ¢ Step 1: Symbol resoluon § Programs define and reference symbols (global variables and func;ons): § void swap() {…} /* define symbol swap */ § swap(); /* reference symbol swap */ § int *xp = &x; /* define symbol xp, reference x */ § Symbol defini;ons are stored in object file (by assembler) in symbol table. § Symbol table is an array of structs § Each entry includes name, size, and locaon of symbol. § During symbol resolu7on step, the linker associates each symbol reference with exactly one symbol definion. Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 7 Carnegie Mellon What Do Linkers Do? (cont) ¢ Step 2: Reloca7on § Merges separate code and data sec;ons into single sec;ons § Relocates symbols from their relave locaons in the .o files to their final absolute memory locaons in the executable. § Updates all references to these symbols to reflect their new posions. Let’s look at these two steps in more detail…. Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 8 Carnegie Mellon Three Kinds of Object Files (Modules) ¢ Relocatable object file (.o file) § Contains code and data in a form that can be combined with other relocatable object files to form executable object file. § Each .o file is produced from exactly one source (.c) file ¢ Executable object file (a.out file) § Contains code and data in a form that can be copied directly into memory and then executed. ¢ Shared object file (.so file) § Special type of relocatable object file that can be loaded into memory and linked dynamically, at either load ;me or run-;me. § Called Dynamic Link Libraries (DLLs) by Windows Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 9 Carnegie Mellon Executable and Linkable Format (ELF) ¢ Standard binary format for object files ¢ One unified format for § Relocatable object files (.o), § Executable object files (a.out) § Shared object files (.so) ¢ Generic name: ELF binaries Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 10 Carnegie Mellon ELF Object File Format ¢ Elf header § Word size, byte ordering, file type (.o, 0 exec, .so), machine type, etc. ELF header ¢ Segment header table Segment header table § Page size, virtual addresses memory segments (required for executables) (sec;ons), segment sizes. .text secon ¢ .text secon .rodata secon § Code .data secon ¢ .rodata sec7on .bss secon § Read only data: jump tables, ... .symtab sec7on ¢ .data secon .rel.txt sec7on § Ini;alized global variables .rel.data sec7on ¢ .bss secon .debug sec7on § Unini;alized global variables § “Block Started by Symbol” Sec7on header table § “BeNer Save Space” § Has sec;on header but occupies no space Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 11 Carnegie Mellon ELF Object File Format (cont.) ¢ .symtab secon 0 § Symbol table ELF header § Procedure and stac variable names Segment header table § Sec;on names and locaons (required for executables) ¢ .rel.text secon .text secon § Relocaon info for .text secon § Addresses of instruc;ons that will need to be .rodata secon modified in the executable .data secon § Instruc;ons for modifying. .bss secon ¢ .rel.data secon § Relocaon info for .data secon .symtab sec7on § Addresses of pointer data that will need to be .rel.txt sec7on modified in the merged executable .rel.data sec7on ¢ .debug secon .debug sec7on § Info for symbolic debugging (gcc -g) ¢ Sec7on header table Sec7on header table § Offsets and sizes of each sec;on Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 12 Carnegie Mellon Linker Symbols ¢ Global symbols § Symbols defined by module m that can be referenced by other modules. § E.g.: non-static C func;ons and non-static global variables. ¢ External symbols § Global symbols that are referenced by module m but defined by some other module. ¢ Local symbols § Symbols that are defined and referenced exclusively by module m. § E.g.: C func;ons and global variables defined with the static aribute. § Local linker symbols are not local program variables Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 13 Carnegie Mellon Step 1: Symbol Resolu7on Referencing a global… …that’s defined here int sum(int *a, int n);! int sum(int *a, int n)! ! {! int array[2] = {1, 2};! int i, s = 0;! ! ! int main()! for (i = 0; i < n; i++) {! {! s += a[i];! int val = sum(array, 2);! }! return val;! return s;! } main.c }! sum.c Defining a global Referencing Linker knows Linker knows a global… nothing of i or s nothing of val …that’s defined here Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 14 Carnegie Mellon Local Symbols ¢ Local non-stac C variables vs. local stac C variables § local non-stac C variables: stored on the stack § local stac C variables: stored in either .bss, or .data int f()! {! static int x = 0;! return x;! Compiler allocates space in .data for }! each definion of x ! int g()! Creates local symbols in the symbol {! table with unique names, e.g., x.1 and static int x = 1;! x.2. return x;! }! Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 15 Carnegie Mellon How Linker Resolves Duplicate Symbol Definions ¢ Program symbols are either strong or weak § Strong: procedures and ini;alized globals § Weak: unini;alized globals p1.c p2.c strong int foo=5; int foo; weak strong p1() { p2() { strong } } Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 16 Carnegie Mellon Linker’s Symbol Rules ¢ Rule 1: Mul7ple strong symbols are not allowed § Each item can be defined only once § Otherwise: Linker error ¢ Rule 2: Given a strong symbol and mul7ple weak symbols, choose the strong symbol § References to the weak symbol resolve to the strong symbol ¢ Rule 3: If there are mul7ple weak symbols, pick an arbitrary one § Can override this with gcc –fno-common Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 17 Carnegie Mellon Linker Puzzles int x; p1() {} p1() {} Link ;me error: two strong symbols (p1) int x; int x; References to x will refer to the same p1() {} p2() {} unini;alized int. Is this what you really want? int x; double x; int y; p2() {} Writes to x in p2 might overwrite y! p1() {} Evil! int x=7; double x; Writes to x in p2 will overwrite y! int y=5; p2() {} Nasty! p1() {} int x=7; int x; References to x will refer to the same ini;alized p1() {} p2() {} variable. Nightmare scenario: two iden7cal weak structs, compiled by different compilers with different alignment rules. Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 18 Carnegie Mellon Global Variables ¢ Avoid if you can ¢ Otherwise § Use static if you can § Ini;alize if you define a global variable § Use extern if you reference an external global variable Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 19 Carnegie Mellon Step 2: Reloca7on Relocatable Object Files Executable Object File System code .text 0 Headers .data System data System code main() .text main.o swap() main() .text More system code int array[2]={1,2} .data System data sum.o .data int array[2]={1,2} sum() .text .symtab .debug Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 20 Carnegie Mellon Reloca7on Entries int array[2] = {1, 2};! ! int main()! {! int val = sum(array, 2);! return val;! } main.c 0000000000000000 <main>:! 0: 48 83 ec 08 sub $0x8,%rsp! 4: be 02 00 00 00 mov $0x2,%esi! 9: bf 00 00 00 00 mov $0x0,%edi # %edi
Recommended publications
  • Architectural Support for Dynamic Linking Varun Agrawal, Abhiroop Dabral, Tapti Palit, Yongming Shen, Michael Ferdman COMPAS Stony Brook University
    Architectural Support for Dynamic Linking Varun Agrawal, Abhiroop Dabral, Tapti Palit, Yongming Shen, Michael Ferdman COMPAS Stony Brook University Abstract 1 Introduction All software in use today relies on libraries, including All computer programs in use today rely on software standard libraries (e.g., C, C++) and application-specific libraries. Libraries can be linked dynamically, deferring libraries (e.g., libxml, libpng). Most libraries are loaded in much of the linking process to the launch of the program. memory and dynamically linked when programs are Dynamically linked libraries offer numerous benefits over launched, resolving symbol addresses across the applica- static linking: they allow code reuse between applications, tions and libraries. Dynamic linking has many benefits: It conserve memory (because only one copy of a library is kept allows code to be reused between applications, conserves in memory for all applications that share it), allow libraries memory (because only one copy of a library is kept in mem- to be patched and updated without modifying programs, ory for all the applications that share it), and allows enable effective address space layout randomization for libraries to be patched and updated without modifying pro- security [21], and many others. As a result, dynamic linking grams, among numerous other benefits. However, these is the predominant choice in all systems today [7]. benefits come at the cost of performance. For every call To facilitate dynamic linking of libraries, compiled pro- made to a function in a dynamically linked library, a trampo- grams include function call trampolines in their binaries. line is used to read the function address from a lookup table When a program is launched, the dynamic linker maps the and branch to the function, incurring memory load and libraries into the process address space and resolves external branch operations.
    [Show full text]
  • CS429: Computer Organization and Architecture Linking I & II
    CS429: Computer Organization and Architecture Linking I & II Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: April 5, 2018 at 09:23 CS429 Slideset 23: 1 Linking I A Simplistic Translation Scheme m.c ASCII source file Problems: Efficiency: small change Compiler requires complete re-compilation. m.s Modularity: hard to share common functions (e.g., printf). Assembler Solution: Static linker (or Binary executable object file linker). p (memory image on disk) CS429 Slideset 23: 2 Linking I Better Scheme Using a Linker Linking is the process of m.c a.c ASCII source files combining various pieces of code and data into a Compiler Compiler single file that can be loaded (copied) into m.s a.s memory and executed. Linking could happen at: Assembler Assembler compile time; Separately compiled m.o a.o load time; relocatable object files run time. Linker (ld) Must somehow tell a Executable object file module about symbols p (code and data for all functions defined in m.c and a.c) from other modules. CS429 Slideset 23: 3 Linking I Linking A linker takes representations of separate program modules and combines them into a single executable. This involves two primary steps: 1 Symbol resolution: associate each symbol reference throughout the set of modules with a single symbol definition. 2 Relocation: associate a memory location with each symbol definition, and modify each reference to point to that location. CS429 Slideset 23: 4 Linking I Translating the Example Program A compiler driver coordinates all steps in the translation and linking process.
    [Show full text]
  • Protecting Against Unexpected System Calls
    Protecting Against Unexpected System Calls C. M. Linn, M. Rajagopalan, S. Baker, C. Collberg, S. K. Debray, J. H. Hartman Department of Computer Science University of Arizona Tucson, AZ 85721 {linnc,mohan,bakers,collberg,debray,jhh}@cs.arizona.edu Abstract writing the return address on the stack with the address of the attack code). This then causes the various actions This paper proposes a comprehensive set of techniques relating to the attack to be carried out. which limit the scope of remote code injection attacks. These techniques prevent any injected code from mak- In order to do any real damage, e.g., create a root ing system calls and thus restrict the capabilities of an shell, change permissions on a file, or access proscribed attacker. In defending against the traditional ways of data, the attack code needs to execute one or more sys- harming a system these techniques significantly raise the tem calls. Because of this, and the well-defined system bar for compromising the host system forcing the attack call interface between application code and the underly- code to take extraordinary steps that may be impractical ing operating system kernel, many researchers have fo- in the context of a remote code injection attack. There cused on the system call interface as a convenient point are two main aspects to our approach. The first is to for detecting and disrupting such attacks (see, for exam- embed semantic information into executables identify- ple, [5, 13, 17, 19, 29, 32, 35, 38]; Section 7 gives a more ing the locations of legitimate system call instructions; extensive discussion).
    [Show full text]
  • Cray XT Programming Environment's Implementation of Dynamic Shared Libraries
    Cray XT Programming Environment’s Implementation of Dynamic Shared Libraries Geir Johansen, Cray Inc. Barb Mauzy, Cray Inc. ABSTRACT: The Cray XT Programming Environment will support the implementation of dynamic shared libraries in future Cray XT CLE releases. The Cray XT implementation will provide the flexibility to allow specific library versions to be chosen at both link and run times. The implementation of dynamic shared libraries allows dynamically linked executables built with software other than the Cray Programming Environment to be run on the Cray XT. KEYWORDS: Cray XT, Programming Environment CUG 2009 Proceedings 1 of 10 information in physical memory that can be shared among 1.0 Introduction multiple processes. This feature potentially reduces the memory allocated for a program on a compute node. This The goal of this paper is provide application analysts advantage becomes more important as the number of a description of Cray XT Programming Environment‟s available cores increases. implementation of dynamic shared libraries. Topics Another advantage of dynamically linked executables covered include the building of dynamic shared library is that the system libraries can be upgraded and take effect programs, configuring the version of dynamic shared without the need to relink applications. The ability to libraries to be used at execution time, and the execution of install library changes without having to rebuild a program a dynamically linked program not built with the Cray XT is a significant advantage in quickly addressing problems, Programming Environment. While a brief description of such as a security issue with a library code. The the Cray XT system implementation of dynamic shared experiences of system administrators have shown that libraries will be given, the Cray XT system architecture codes using dynamic shared libraries are more resilient to and system administration issues of dynamic shared system upgrades than codes using static libraries [1].
    [Show full text]
  • Dynamic Linking Considered Harmful
    DYNAMIC LINKING CONSIDERED HARMFUL 1 WHY WE NEED LINKING ¡ Want to access code/data defined somewhere else (another file in our project, a library, etc) ¡ In compiler-speak, “we want symbols with external linkage” § I only really care about functions here ¡ Need a mechanism by which we can reference symbols whose location we don’t know ¡ A linker solves this problem. Takes symbols annotated by the compiler (unresolved symbols) and patches them 2 DYNAMIC LINKING ¡ We want to: ¡ use code defined somewhere else, but we don’t want to have to recompile/link when it’s updated ¡ be able to link only those symbols used as runtime (deferred/lazy linking) ¡ be more efficient with resources (may get to this later) 3 CAVEATS ¡ Applies to UNIX, particularly Linux, x86 architecture, ELF Relevant files: -glibcX.X/elf/rtld.c -linux-X.X.X/fs/exec.c, binfmt_elf.c -/usr/include/linux/elf.h ¡ (I think) Windows linking operates similarly 4 THE BIRTH OF A PROCESS 5 THE COMPILER ¡ Compiles your code into a relocatable object file (in the ELF format, which we’ll get to see more of later) ¡ One of the chunks in the .o is a symbol table ¡ This table contains the names of symbols referenced and defined in the file ¡ Unresolved symbols will have relocation entries (in a relocation table) 6 THE LINKER ¡ Patches up the unresolved symbols it can. If we’re linking statically, it has to fix all of them. Otherwise, at runtime ¡ Relocation stage. Will not go into detail here. § Basically, prepares program segments and symbol references for load time 7 THE SHELL fork(), exec() 8 THE KERNEL (LOADER) ¡ Loaders are typically kernel modules.
    [Show full text]
  • Linker Script Guide Emprog
    Linker Script Guide Emprog Emprog ThunderBench™ Linker Script Guide Version 1.2 ©May 2013 Page | 1 Linker Script Guide Emprog 1 - Linker Script guide 2 -Arm Specific Options Page | 2 Linker Script Guide Emprog 1 The Linker Scripts Every link is controlled by a linker script. This script is written in the linker command language. The main purpose of the linker script is to describe how the sections in the input files should be mapped into the output file, and to control the memory layout of the output file. Most linker scripts do nothing more than this. However, when necessary, the linker script can also direct the linker to perform many other operations, using the commands described below. The linker always uses a linker script. If you do not supply one yourself, the linker will use a default script that is compiled into the linker executable. You can use the ‘--verbose’ command line option to display the default linker script. Certain command line options, such as ‘-r’ or ‘-N’, will affect the default linker script. You may supply your own linker script by using the ‘-T’ command line option. When you do this, your linker script will replace the default linker script. You may also use linker scripts implicitly by naming them as input files to the linker, as though they were files to be linked. 1.1 Basic Linker Script Concepts We need to define some basic concepts and vocabulary in order to describe the linker script language. The linker combines input files into a single output file. The output file and each input file are in a special data format known as an object file format.
    [Show full text]
  • Introduction to Computer Systems 15-213/18-243, Spring 2009
    Carnegie Mellon Linking 15-213: Introduction to Computer Systems 13th Lecture, February 23rd, 2016 Instructors: Franz Franchetti & Seth Copen Goldstein, Ralf Brown, and Brian Railing Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 1 Carnegie Mellon Today Linking Case study: Library interpositioning Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2 Carnegie Mellon Example C Program int sum(int *a, int n); int sum(int *a, int n) { int array[2] = {1, 2}; int i, s = 0; int main() for (i = 0; i < n; i++) { { s += a[i]; int val = sum(array, 2); } return val; return s; } } main.c sum.c Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 3 Carnegie Mellon Static Linking Programs are translated and linked using a compiler driver: . linux> gcc -Og -o prog main.c sum.c . linux> ./prog main.c sum.c Source files Translators Translators (cpp, cc1, as) (cpp, cc1, as) main.o sum.o Separately compiled relocatable object files Linker (ld) Fully linked executable object file prog (contains code and data for all functions defined in main.c and sum.c) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 4 Carnegie Mellon Why Linkers? Reason 1: Modularity . Program can be written as a collection of smaller source files, rather than one monolithic mass. Can build libraries of common functions (more on this later) . e.g., Math library, standard C library Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 5 Carnegie Mellon Why Linkers? (cont) Reason 2: Efficiency .
    [Show full text]
  • Linkers and Loaders Do?
    Linkers & Loaders by John R. Levine Table of Contents 1 Table of Contents Chapter 0: Front Matter ........................................................ 1 Dedication .............................................................................................. 1 Introduction ............................................................................................ 1 Who is this book for? ......................................................................... 2 Chapter summaries ............................................................................. 3 The project ......................................................................................... 4 Acknowledgements ............................................................................ 5 Contact us ........................................................................................... 6 Chapter 1: Linking and Loading ........................................... 7 What do linkers and loaders do? ............................................................ 7 Address binding: a historical perspective .............................................. 7 Linking vs. loading .............................................................................. 10 Tw o-pass linking .............................................................................. 12 Object code libraries ........................................................................ 15 Relocation and code modification .................................................... 17 Compiler Drivers .................................................................................
    [Show full text]
  • Let Your Mach-O fly
    Let your Mach-O fly Vincenzo Iozzo [email protected] January 15, 2009 Abstract Mac OS X is starting to spread among users, so new exploitation techniques have to be discovered. Even if a lot of interesting ways of exploitation on OSX were presented in the past, the lack of anti-forensics techniques is clear. The talk is focused on an in-memory injection technique. Specifically, how it is possible to inject into a victim's machine any kind of binaries ranging from your own piece of code to real applications like Safari. This is accomplished without leaving traces on the hard disk and without creating a new process, since the whole exploitation is performed in memory. If an attacker is able to execute code in the target machine, it is possible to run this attack instead of a classic shellcode and to use it as a trampoline for higher-lever payloads. Other similar payloads like meterpreter or meterpretux[11] exist but none of them is able to run on Mac OS X. Besides, many of these techniques require to run specific crafted binaries, that way precompiled applications are left out from the possible range of payloads. 1 Introduction Mac OS X is constantly attracting new users. Given that fact, in the last few years researchers focused more and more on this topic highlighting a number of security concerns, mainly caused by the lack of modern security counter mea- sures. Nonetheless there is a lack of advanced techniques and tools that are already present and used on other OSes instead.
    [Show full text]
  • Mach-O Programming Topics
    Mach-O Programming Topics Tools > Compiling & Debugging 2006-11-28 subsidiaries in the United States and other Apple Inc. countries. © 2003, 2006 Apple Computer, Inc. Java and all Java-based trademarks are All rights reserved. trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other No part of this publication may be countries. reproduced, stored in a retrieval system, or transmitted, in any form or by any means, PowerPC and and the PowerPC logo are mechanical, electronic, photocopying, trademarks of International Business recording, or otherwise, without prior Machines Corporation, used under license written permission of Apple Inc., with the therefrom. following exceptions: Any person is hereby UNIX is a registered trademark of The Open authorized to store documentation on a Group single computer for personal use only and Simultaneously published in the United to print copies of documentation for States and Canada. personal use provided that the documentation contains Apple’s copyright Even though Apple has reviewed this document, APPLE MAKES NO WARRANTY OR notice. REPRESENTATION, EITHER EXPRESS OR IMPLIED, WITH RESPECT TO THIS The Apple logo is a trademark of Apple Inc. DOCUMENT, ITS QUALITY, ACCURACY, MERCHANTABILITY, OR FITNESS FOR A Use of the “keyboard” Apple logo PARTICULAR PURPOSE. AS A RESULT, THIS (Option-Shift-K) for commercial purposes DOCUMENT IS PROVIDED “AS IS,” AND YOU, THE READER, ARE ASSUMING THE without the prior written consent of Apple ENTIRE RISK AS TO ITS QUALITY AND may constitute trademark infringement and ACCURACY. unfair competition in violation of federal IN NO EVENT WILL APPLE BE LIABLE FOR and state laws.
    [Show full text]
  • “Hello, World”* (*But Were Afraid to Ask.)
    Everything you ever wanted to know about “hello, world”* (*but were afraid to ask.) Brooks Davis SRI International <[email protected]> 1 Abstract compile with a modern compiler in C11 mode. In practice we need to specify a return type (or at least The first example in the classic book “The C Pro- void for returning nothing) and arguments. In the- gramming Language” by Kernighan and Ritchie[1] ory the main() function is defined to return an inte- is in fact a remarkably complete test of the C pro- ger and take two arguments for the command line gramming language. This paper provides a guided argument list; however, compilers tend to accept tour of a slightly more complex program, where this alternative form for compatibility sake. printf() is called with multiple arguments. Along #include <stdio.h> the way from the initial processes’s call to exec() void to the final _exit(), we’ll tour the program loading main (void) code in the kernel, the basics of system-call imple- { mentation, the implementation of the memory allo- printf ("hello, world\n"); cator, and of course printf(). We’ll also touch on } localization, a little on threading support, and a brief One of the more interesting features of the overview of the dynamic linker. printf() function is that it takes a variable number of arguments. Varidac functions such as printf() 2 Introduction are both interesting and error prone, so it’s useful to understand how they work. As such, we’ll use the The first example in the classic K&R C book is a following enhanced version of helloworld with a simple program that prints the text “hello, world” string argument and an integer argument.
    [Show full text]
  • Converting Static Libraries to Dynamic Libraries for Ios
    Converting Static Libraries to Dynamic Libraries for iOS This is a tutorial that will demonstrate two methods for converting a static library into a dynamic library for use on iOS and OS X. If you are attempting to do this with a library that is available via CocoaPods, then I recommend that you use CocoaPods to do this work for you. The methods I am going to describe should only be used if there are no other options. ##What are static and dynamic libraries? First thing to understand is the differences between using static and dynamic libraries. I have a previous blog post that goes into detail about this. You should be aware that there have been some performance impacts with loading many dynamic libraries when an app gets launched. This is due to the need for the amfi daemon to read and validate the contents of the library files to ensure their code signatures match the signing identify of the app that is asking for them to be loaded. Once that is done the library can be safely opened by the dynamic linker and loaded into memory for the app. For the methods, we are going to use a sample library that was used in the linked post above: foo.h #ifndef __foo__bar__ #define __foo__bar__ #include <stdio.h> int fizz(); #endif /* defined(__foo__bar__) */ foo.c #include "foo.h" #include <CoreFoundation/CoreFoundation.h> int fizz() { CFShow(CFSTR("buzz")); return 0; } To follow along with the example, I am going to generate a static library from the library code by calling: $ clang -c foo.c -o foo.o $ libtool -static foo.o -o libfoo.a -framework CoreFoundation ##Method 1: Manual Conversion The scenario is that you get a static library as part of a third-party SDK you have to integrate and you are currently building your app for iOS 8 and above.
    [Show full text]