A tale of ELFs and DWARFs

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

Shared Libraries: The Dynamic Linker

Outline. Outline. Common Linux tools to explore object/executable files. Revealing Internals of Loader. Zhiqiang Lin

Today s Big Adventure

Dynamic libraries explained

Today s Big Adventure

Incremental Linking with Gold

Embedded Systems Programming

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

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

Lab 1: First Steps in C++ - Eclipse

Linkers and Loaders. CS 167 VI 1 Copyright 2008 Thomas W. Doeppner. All rights reserved.

CS140 - Summer Handout #8

Department of Computer Science and Engineering Yonghong Yan

Lecture 4 Processes. Dynamic Analysis. GDB

An Evil Copy: How the Loader Betrays You

CS5460/6460: Operating Systems. Lecture 21: Shared libraries. Anton Burtsev March, 2014

CS240: Programming in C

Fixing/Making Holes in Binaries

2012 LLVM Euro - Michael Spencer. lld. Friday, April 13, The LLVM Linker

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.

Executables and Linking. CS449 Spring 2016

CS631 - Advanced Programming in the UNIX Environment

Assembly Language Programming Linkers

An application: foreign function bindings

Link 8.A Dynamic Linking

Reminder: compiling & linking

lld: A Fast, Simple and Portable Linker Rui Ueyama LLVM Developers' Meeting 2017

ENGR/CS 101 CS Session Lecture 3

CS 33. Libraries. CS33 Intro to Computer Systems XXVIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

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

Fast Introduction to Object Oriented Programming and C++

Process Address Spaces and Binary Formats

C03c: Linkers and Loaders

Domain Specific Debugging Tools

QUIZ. What is wrong with this code that uses default arguments?

MPLAB XC8 C Compiler Version 2.00 Release Notes for AVR MCU

Separate Compilation Model

The Software Stack: From Assembly Language to Machine Code

CS 33. Libraries. CS33 Intro to Computer Systems XXIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Overview. The Whited Sepulchre Four Strings to a Bow λ-connectedness

ECE 598 Advanced Operating Systems Lecture 10

Retargetable Binary Utilities

Process Address Spaces and Binary Formats

RELEASE NOTES: GNURL78 v KPIT Technologies Limited is releasing the GNURL78 v15.02, a cross compiler tool for Renesas RL78 micro-controllers.

Download, Install and Setup the Linux Development Workload Create a New Linux Project Configure a Linux Project Configure a Linux CMake Project

CSCI341. Lecture 22, MIPS Programming: Directives, Linkers, Loaders, Memory

My ld.so. Version 1 5 December Epita systems/security laboratory 2018

Link 8. Dynamic Linking

C and C++ 2. Functions Preprocessor. Alan Mycroft

Protecting Against Unexpected System Calls

Today s Big Adventure

CIT 595 Spring System Software: Programming Tools. Assembly Process Example: First Pass. Assembly Process Example: Second Pass.

Compiler (1A) Young Won Lim 6/8/14

Introducing C++ David Chisnall. March 15, 2011

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Programmazione. Prof. Marco Bertini

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford.

Midterm results Mean: 43, median: 40

Re-entrant code and Libraries

Memory and C/C++ modules

Cake: a tool for adaptation of object code

CS 107 Lecture 18: GCC and Make

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

Memory and C/C++ modules

Overview of C++ Support in TI Compiler Tools July 2008

In I t n er t a er c a t c i t v i e v C+ C + + Com C pilat a ion on (RE ( PL RE ) PL : ) Th T e h L e ean L ean W a W y a by Viktor Kirilov 1

Executables and Linking. CS449 Fall 2017

Compilation, Disassembly, and Profiling (in Linux)

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

The gnu Binary Utilities

[07] SEGMENTATION 1. 1

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

How to cross compile with LLVM based tools. Peter Smith, Linaro

CS2141 Software Development using C/C++ Libraries

Compilation/linking revisited. Memory and C/C++ modules. Layout of C/C++ programs. A sample C program demo.c. A possible structure of demo.

Lec 13: Linking and Memory. Kavita Bala CS 3410, Fall 2008 Computer Science Cornell University. Announcements

QUIZ. Can you find 5 errors in this code?

CPEG421/621 Tutorial

Giridhar Ravipati University of Wisconsin, Madison. The Deconstruction of Dyninst: Part 1- the SymtabAPI

Red Hat Developer Toolset 6.1

Programming Tips for CS758/858

Romain Thomas - Static instrumentation based on executable file formats

Obtained the source code to gcc, one can just follow the instructions given in the INSTALL file for GCC.

Essentials for Scientific Computing: Source Code, Compilation and Libraries Day 8

MODERN AND LUCID C++ ADVANCED

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

Tools of the Trade The C Language Laboration 04. Outline. 1 Tools of the Trade. 2 The C Language. 3 Laboration 04

Red Hat Enterprise Linux C++ Toolchains: 10 Tips to Drive Your Development

The GNU linker. ld (GNU Binutils) Version Steve Chamberlain Ian Lance Taylor

How To Write Shared Libraries

Computer Organization & Assembly Language Programming (CSE 2312)

Compiler, Assembler, and Linker

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

CS 240 Final Exam Review

Fundamentals of Programming Session 19

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

System V Application Binary Interface Linux Extensions Version 0.1

Introduction p. 1 Why Linux? p. 2 Embedded Linux Today p. 3 Open Source and the GPL p. 3 Free Versus Freedom p. 4 Standards and Relevant Bodies p.

Transcription:

A tale of ELFs and DWARFs A glimpse into the world of linkers, loaders and binary formats Volker Krause vkrause@kde.org @VolkerKrause

Our Workflow Write code Run compiler... Run application Profit!

Why care? Understanding linker errors Debugging weird runtime behavior Performance Surviving without CMake convenience

The Magic Build time combine multiple object files into one library or executable Run time find all needed libraries map them into memory resolve dynamic symbols call the entry point functions

Linker Errors undefined reference to `Class::method()' -Wl,--fatal-warnings -Wl,--no-undefined Catch missing symbols at compile time Causes Method is declared but not defined Signature mismatch Using a method that is not exported

Looking Into Libraries Binutils: nm, readelf, objdump Mac: otool Windows: Dependency Walker 000000000003a344 T _ZNK4KJob11errorStringEv 0000000000039fc8 T _ZNK4KJob11isSuspendedEv

Name Mangling For C: just using the method name extern C { } C++: overloading, templates, namespaces,... Information included: argument types, cv-qualifiers library name (Mach-O two-level lookup) public/protected/private (MSVC) Demangle: c++filt, nm -C

Export Macros -fvisibility=hidden UNIX attribute ((visibility("default"))) Windows declspec(dllexport) declspec(dllimport) Upper-case letters in nm output indicate exported symbols

File Formats ELF (Linux), Mach-O (Mac), PE (Windows) Simple, designed for zero-copy mapping into memory Essentially the same for libraries and executables Consist of multiple sections, custom sections possible Symbol table Text (executable code) Data (string literals, QRC, )

Qt5 Plug-ins Implementation hidden by moc Plug-in meta-data in custom section Contains a QJsonObject in binary format: qt_pluginmetadata attribute ((section(".qtmetadata"))) readelf --sections plugin.so Plug-in entry symbol (must be exported!) nm -C plugin.so grep qt_plugin_instance

Debug Information DWARF Custom section or separate file -g[0-3] decides level of detail Index for faster loading in GDB: (gdb) save gdb-index <lib> objcopy \ --add-section.gdb_index=<lib>.gdb-index \ --set-section-flags.gdb_index=readonly \ <lib> <lib> With gold: --gdb-index

Finding Libraries Search paths: RPATH if RUNPATH not set (-rpath, chrpath) LD_LIBRARY_PATH RUNPATH (--enable-new-dtags) /etc/ld.so* Diagnostics: Static: ldd, readelf -d <elf> Dynamic: LD_DEBUG=libs, strace

Does size matter? mmap'ed in 3 sections: Read-only Read-only + executable Read-write Read-only sections are shared Writable sections use copy-on-write

Code Size

Writable Data Keep small, this cannot be shared const char *c1 = "..."; const char c2[] = "..."; static const char *c3 = "..."; char* c4; Verify with nm: r vs. d or b 0000000000601040 D c1 0000000000400633 r c2 0000000000601048 d c3 0000000000601060 B c4

Read-only Data const char *data = hello world ; int main(int, char**) { const_cast<char*>(data)[0] = 'H'; } Valgrind: Process terminating with default action of signal 11 (SIGSEGV) Bad permissions for mapped region at address 0x400684 at 0x4005F0: main (main.cpp:3)

Relocation Procedure Linkage Table (PLT) Stubs for calling external functions Global Offset Table (GOT) Filled during dynamic linking Contains addresses of external symbols Calling another function: Intra-library: relative jump (-Bsymbolic) Inter-library: PLT stub + GOT

Qt5 PMF Connections Function pointer connect() Compares pointer to signal with moc data Must avoid comparison between PLT stub and actual function... Addressed by -fpic/-fpie Safety check in qglobal.h enforces that On ARM: possibly also -pie due to toolchain bug

Conclusion CMake hides most of this from us Nasty to debug if something goes wrong To learn more, read Ulrich Drepper's How to write shared libraries

Questions?

References John R. Levine: Linkers and Loaders, Morgan-Kauffman, ISBN 1-55860-496-0. 2000 http://www.iecc.com/linker/ Ulrich Drepper: How To Write Shared Libraries. 2011 http://www.akkadia.org/drepper/dsohowto.pdf Slides & code: http://www.kdab.com/~volker/akademy/2014/ kde:scratch/vkrause/elf-dissector.git