Binary compatibility for library developers. Thiago Macieira, Qt Core Maintainer LinuxCon North America, New Orleans, Sept. 2013

Similar documents
C++ INTERVIEW QUESTIONS

How to use PDFlib products with PHP

Open Governance for Tizen 3.0

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

C++ Programming Language

1 Abstract Data Types Information Hiding

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

sys socketcall: Network systems calls on Linux

C Programming Review & Productivity Tools

Application Note C++ Debugging

A deeper look at Inline functions

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

LLVMLinux: Embracing the Dragon

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Java Interview Questions and Answers

4D Plugin SDK v11. Another minor change, real values on 10 bytes is no longer supported.

Applying Clang Static Analyzer to Linux Kernel

Coding conventions and C++-style

Sources: On the Web: Slides will be available on:

OpenCL Static C++ Kernel Language Extension

How To Write Portable Programs In C

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

An Overview of Java. overview-1

The C Programming Language course syllabus associate level

CIS 190: C/C++ Programming. Polymorphism

MSP430 C/C++ CODE GENERATION TOOLS Compiler Version 3.2.X Parser Error/Warning/Remark List

Generating Serialisation Code with Clang

Image Acquisition Toolbox Adaptor Kit User's Guide

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

C++ Overloading, Constructors, Assignment operator


How to create PDF maps, pdf layer maps and pdf maps with attributes using ArcGIS. Lynne W Fielding, GISP Town of Westwood

Release Notes. Asset Control and Contract Management Solution 6.1. March 30, 2005

Motorola 8- and 16-bit Embedded Application Binary Interface (M8/16EABI)

Installing (1.8.7) 9/2/ Installing jgrasp

An Incomplete C++ Primer. University of Wyoming MA 5310

Object Oriented Software Design II

Declarative API for Defining Visualizations in Envision

Qt Signals and Slots. Olivier Goffart. October 2013

Common Errors in C/C++ Code and Static Analysis

6.S096 Lecture 1 Introduction to C

TNM093 Practical Data Visualization and Virtual Reality Laboratory Platform

UML for C# Modeling Basics

Talend Component: tjasperreportexec

1 Posix API vs Windows API

Applied Informatics C++ Coding Style Guide

Porting Lustre to Operating Systems other than Linux. Ken Hornstein US Naval Research Laboratory April 16, 2010

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

Bypassing Memory Protections: The Future of Exploitation

Get an Easy Performance Boost Even with Unthreaded Apps. with Intel Parallel Studio XE for Windows*

Embedded Software Development with MPS

Surface and Volumetric Data Rendering and Visualisation

Supported platforms & compilers Required software Where to download the packages Geant4 toolkit installation (release 9.6)

Building Applications Using Micro Focus COBOL

Variable Base Interface

Software Configuration Management

Developing In Eclipse, with ADT

Next, the driver runs the C compiler (cc1), which translates main.i into an ASCII assembly language file main.s.

Moving from CS 61A Scheme to CS 61B Java

Lazy OpenCV installation and use with Visual Studio

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Oracle Taleo Business Edition Cloud Service. What s New in Release 15B1

Advanced Encryption Standard (AES) User's Guide

Porting CodeWarrior Projects to Xcode. (Legacy)

Constructor, Destructor, Accessibility and Virtual Functions

Distributed Version Control

Graphical Environment Tool for Development versus Non Graphical Development Tool

Course MS10975A Introduction to Programming. Length: 5 Days

Excel Formatting: Best Practices in Financial Models

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Android NDK Native Development Kit

What s New in OMNeT++

How do Users and Processes interact with the Operating System? Services for Processes. OS Structure with Services. Services for the OS Itself

SECTION 5: Finalizing Your Workbook

MADRAS: Multi-Architecture Binary Rewriting Tool Technical report. Cédric Valensi University of Versailles Saint-Quentin en Yvelines

This document presents the new features available in ngklast release 4.4 and KServer 4.2.

Outside In Image Export Technology SDK Quick Start Guide

Migration of Process Credentials

Xcode Project Management Guide. (Legacy)

El Dorado Union High School District Educational Services

FileMaker Pro and Microsoft Office Integration

Illustration 1: Diagram of program function and data flow

Authoring for System Center 2012 Operations Manager

Frequently Asked Questions Sage Pastel Intelligence Reporting

Introduction to Data Structures

13 Classes & Objects with Constructors/Destructors

Part I. The Picture class

CMSC 427 Computer Graphics Programming Assignment 1: Introduction to OpenGL and C++ Due Date: 11:59 PM, Sept. 15, 2015

C and C++: Case Studies in Compatibility

MAXA-COOKIE-MANAGER - USER MANUAL - SW-Release V 5.0 / Document Rev. 1.1

How To Upgrade To Sugarcrm 7.X

CSC230 Getting Starting in C. Tyler Bletsch

3F6 - Software Engineering and Design. Handout 10 Distributed Systems I With Markup. Steve Young

Transcription:

Binary compatibility for library developers Thiago Macieira, Qt Core Maintainer LinuxCon North America, New Orleans, Sept. 2013

Who am I? Open Source developer for 15 years C++ developer for 13 years Software Architect at Intel s Open Source Technology Center (OTC) Maintainer of two modules in the Qt Project QtCore and QtDBus MBA and double degree in Engineering Previously, led the Qt Open Governance project 2

Definitions Binary compatibility Source compatibility Behaviour compatibility Bug compatibility 6

Binary compatibility Two libraries are binary compatible with each other if: Programs compiled against one will load and run correctly* against the other * by some definition of correct 7

Source compatibility Two libraries are source compatible with each other if: Source code written against one will compile without changes against the other 8

Behaviour and bug compatibility Two libraries are behaviour-compatible with each other if: The program will exhibit the same behaviour with either library Two libraries are bugcompatible with each other if: Expanded version of behaviour compatibility to include buggy behaviour 9

Forwards and backwards Depends on the point of view Backwards compatibility: newer version retains compatibility with older version You can upgrade the library Forwards compatibility: older version foreshadows compatibility with newer version You can downgrade the library 10

This presentation focuses on Backwards binary compatibility This depends on the ABI Will focus on the System V ELF ABI for Linux and IA-64 C++ ABI 11

Why you should care Library used by libraries They expose your API in their API Their users might want to use a newer version of your library Library used by anything Upgrading parts of the system Large, complex project 12

Project with 2 modules: initial state Mod 1 Mod 2 Data exchange Your lib Your lib 14

Lib is upgraded in one module Mod 1 (recompiled) Data exchange Mod 2 Your lib v2 Your lib Does this still load? 15

Co-existing libraries Mod 1 Mod 2 Data exchange Your lib libv2 Your lib Does it still work? 16

If you re developing an application... This does not apply to you Except if the application has plugins Or if it has independent modules The application has libraries 17

The details

Binary compatibility requires... No public¹ symbol be removed All public¹ functions retain retain their properties Which arguments are passed in registers, which are passed on the stack, implicit arguments, argument count, etc. All public¹ structures retain their layout and properties For both C and C++ aggregates: sizeof, alignof, order & type of publiclyaccessible members, etc. For C++ aggregates: dsize, nvsize, PODness, etc. 19 1) Symbols intentionally made public as part of the API plus private symbols used in inline functions

Example: simple C library (C++ comes later) /* lib-header.h. */ struct Data { int i; int j; }; extern struct Data global_data; void function(struct Data *data); /* lib-source.c */ $ gcc -c /tmp/lib-source.c $ nm lib-source.o 00000000 T function 00000000 D global_data Could be B too struct Data global_data = { 1 }; void function(struct Data *data) { } 20

No public symbol is removed Easy to do Do not remove any variables or functions that exist Do not change any variable or function in a way that would cause its external (mangled) name to change In our example, we cannot: Remove either the function function or the variable global_data 21

All functions retain their properties The C++ language helps you This requirement is mostly fulfilled by the previous and next requirements If the data types retain their properties And if the mangled name of a function is retained The function retains its properties In C, it s possible to change the arguments without changing the external symbol In our example: The function function must not read more than 1 argument of integral type 22

All data types retain their properties Can be automated with a C or C++ parser and the compiler Best avoided: Use opaque types / d-pointers / private implementation Examples: Change alignment user s structure could add or remove padding Change non-padded size the compiler is allowed to use tail-padding (C++) Make non-pod user s structure becomes non-pod too In our example, we cannot: Reorder the members in the struct Remove the members Place the members in a union with a long long (changes the alignment) 23

And then there s C++ Life gets more complicated External names for all¹ functions are mangled External names for all¹ variables in namespaces are mangled In some other ABIs, even those in the base namespace Non-POD (Plain Old Data) aggregates have more rules But we gain too: Functions can be overloaded Mangled names help in ensuring binary compatibility for functions 24 1) all except those declared with extern C

C++ mangled names IA-64 C++ ABI Prefixed by _Z Case sensitive Doesn t mangle free variables Mangles only what is required for overloads that can co-exist Microsoft Visual Studio Prefixed by question mark (?) Case insensitive Mangles free variables Mangles everything, including: Return type Struct vs class Public, protected, private Near, far, 64-bit pointers cv-qualifiers 25

Example: our library in C++ /* lib-header.h. */ struct Data { int i; int j; }; extern struct Data global_data; void function(struct Data *data); /* lib-source.cpp */ struct Data global_data = { 1 }; void function(struct Data *data) { } $ g++ -c /tmp/lib-source.cpp $ nm lib-source.o 00000000 T _Z8functionP4Data 00000000 D global_data Note the presence of Data 26

What works

Guidelines Don t expose what you don t need Be conservative in what you change Follow the Binary Compatibility with C++ [1] guidebook Use automated test tools 35 [1] http://techbase.kde.org/policies/binary_compatibility_issues_with_c++

Minimal exported API Design a minimal API If you re unsure about something, don t include it (yet) Limit exports by using ELF symbol visibility: -fvisibility=hidden -fvisibility-inlines-hidden attribute ((visibility( default ))) Use opaque or simple types Private implementation, d-pointers Use an API based on functions Avoid exported variables Avoid returning pointers or C++ references to internal variables 36

Why functions and private implementations? In C Use opaque types typedef struct _GDir GDir; Forbidden! Can t be constructed by the user always passed by pointer Free to be changed at will In C++ Use private implementation Your public types won t change much or at all Lowers the risk of changing the type s properties You can freely change the private implementation Adding new functions is easier than modifying the public type C99 7.1.3p1: All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use 37

Changing non-virtual functions¹ (C and C++) You can Add a new function De-inline an existing function If it s acceptable that the old copy be run Remove a private function If it has never been called in an inline function, ever (C++) Change default parameters You cannot Unexport or remove public functions Inline an existing function (C) Change the parameter so it would be passed differently (C++) Change its signature: Change or add parameters Change cv-qualifier Change access rights Change return type 38 1) includes all C functions as well as C++ functions with extern C

Changing virtual functions (C++ only) You can Override an existing virtual Only from primary, non-virtual base Add a new virtual to a leaf (final) class You cannot Add or remove a virtual to a non-final class Change the order of the declarations Add a virtual to a class that had none 39

Anchoring the virtual table (C++ only) Make sure there s one non-inline virtual Preferably the destructor Avoid virtuals in template classes 40

Changing non-static members in aggregates (C and C++) You can Rename private members¹ Repurpose private members² Add new members to the end, provided the struct is: POD (C++98 and all C structs) Standard-layout (C++11) and: (C++) The constructor is private; OR The struct has a member containing its size You cannot Reorder public members in any way Remove members You should not (C++) Change member access privileges (C++) Add a reference or const or non-pod member to a struct without one 41 1) provided the class has no friends; 2) provided old inline functions still work

Testing compliance Run automated tests frequently Run full tests at least once before the release On Windows: use the exports file On Unix: use nm, otool (Mac), readelf (ELF systems) GCC: use -fdump-class-hierarchy Everywhere: use the Linux Foundation s ABI Compliance Checker[1] Confirmed to run on Mac, Windows and FreeBSD 42 [1] http://ispras.linuxbase.org/index.php/abi_compliance_checker

Manual checking before release Do a header diff git diff --diff-filter=m oldtag -- \*.h Manually exclude headers that aren t installed Or obtain the list of installed headers from your buildsystem 43

Be careful with false positives You probably want a white and black list White-list your library s own API Black-list leaked symbols from other libraries Inlines and unanchored virtual tables 44

Further: experimental API Don t do it like ICU Place it in a separate library In fact, place it in a separate source release Keeps Linux distributions happy 45

Further: breaking binary compatibility Announce in well in advance Keep previous version maintained for longer than usual Try to keep source compatibility Change your library names (ELF soname) 46

Resources Binary compatibility guide in KDE Techbase: http://techbase.kde.org/policies/binary_compatibility_issues_with_c++ Examples: http://techbase.kde.org/policies/binary_compatibility_examples Calling convention article (includes MSVC, Sun CC): http://www.agner.org/optimize/calling_conventions.pdf IA-64 / Cross-platform C++ ABI: http://mentorembedded.github.io/cxx-abi/abi.html http://refspecs.linux-foundation.org/cxxabi-1.86.html How to Write Shared Libraries, by Ulrich Drepper http://www.akkadia.org/drepper/dsohowto.pdf libabc https://git.kernel.org/cgit/linux/kernel/git/kay/libabc.git 47

Thiago Macieira thiago.macieira@intel.com