Learning how to write C/C++ code is only the first step. To be a serious programmer, you need to understand the structure and purpose of the binary files produced by the compiler: object files, static libraries, shared libraries, and, of course, executables. Advanced C and C++ Compiling explains the build process in detail and shows how to integrate code from other developers in the form of deployed libraries as well as how to resolve issues and potential mismatches between your own and external code trees. With the proliferation of open source, understanding these issues is increasingly the responsibility of the individual programmer. Advanced C and C++ Compiling brings all of the information needed to move from intermediate to expert programmer together in one place -- an engineering guide on the topic of C/C++ binaries to help you get the most accurate and pertinent information in the quickest possible time. What you’ll learn The details of the build process, including compiling and linking The inner workings of static libraries, shared libraries, and executables Ways to properly architect code for smooth integration of future changes Tips for troubleshooting problems with compiling and linking as well as run-time problems How to use operating system-specific (Linux and Windows) tools for analysis of binary files
Author(s): Milan Stevanovic
Edition: 1st
Publisher: Apress
Year: 2014
Language: English
Pages: 326
Tags: Библиотека;Компьютерная литература;C / C++;
Advanced C and C++
Compiling......Page 313
Contents at a Glance......Page 3
Contents......Page 316
About the Author......Page 323
About the Technical Reviewers......Page 324
Acknowledgment......Page 325
Introduction......Page 4
Useful Abstractions......Page 7
Memory Hierarchy and Caching Strategy......Page 8
Virtual Memory......Page 9
Virtual Addressing......Page 11
Process Memory Division Scheme......Page 12
Summary......Page 13
Initial Assumptions......Page 14
Concept illustration: Demo Project......Page 15
Introductory Definitions......Page 16
The Stages of Compiling......Page 17
Demo Project Preprocessing Example......Page 18
Linguistic Analysis......Page 19
AT&T Assembly Format Example......Page 20
Intel Assembly Format Example......Page 22
Optimization......Page 23
Demo Project Compiling Example......Page 24
Object File Properties......Page 31
Compilation Process Limitations......Page 32
What Makes Section Combining so Complicated?......Page 33
Relocation......Page 34
Resolving References......Page 35
Demo Project Linking Example......Page 37
Linker’s Viewpoint......Page 40
Executable File Properties......Page 42
Variety of Section Types......Page 43
A Variety of Symbol Types......Page 45
Importance of the Shell......Page 47
Loader-Specific View of a Binary File (Sections vs. Segments)......Page 49
Program Loading Stage......Page 51
The Loader Finds the Entry Point......Page 53
The Role of __libc_start_main() Function......Page 54
Functions Calling Conventions......Page 55
Static Libraries......Page 56
Dynamic vs. Shared Libraries......Page 59
Part 1: Building the Dynamic Library......Page 62
Part 2: Playing by Trust While Building the Client Executable (Looking for the Symbols Only)......Page 63
Part 3: Runtime Loading and Symbol Resolution......Page 64
Special Binary File Types Related to Dynamic Linking in Windows......Page 65
Export File (.exp)......Page 66
Application Binary Interface (ABI)......Page 67
Import Selectiveness Criteria for Static Libraries......Page 68
Whole Archive Import Scenario......Page 69
Final Verdict......Page 71
Useful Comparison Analogies......Page 74
The Conclusion: The Impact of Binary Reuse Concept......Page 75
Creating a Windows Static Library......Page 77
Recommended Use Case Scenarios......Page 78
Potential for Losing the Symbol Visibility and Uniqueness......Page 79
Specific Rules of Linking Static Libraries......Page 80
Static Libraries Issues on 64-bit Linux......Page 81
Resolving the Problem In Real Life Scenarios......Page 82
About the -fPIC Compiler Flag......Page 83
Question 3: Is the use of the -fPIC compiler flag strictly confined to the domain of dynamic libraries? Can it be used when.........Page 84
Creating the Dynamic Library in Windows......Page 85
Designing the Binary Interface......Page 89
Issue #1: C++ Imposes More Complex Symbol Name Requirements......Page 90
Issue #2: Static Initialization Order Fiasco......Page 91
Problem Description......Page 92
Issue #3: Templates......Page 93
Guideline #4: Use a Class Factory Mechanism (C++) or Module (C)......Page 94
Exporting the Linux Dynamic Library Symbols......Page 96
The Symbol Export Control Demo Example......Page 97
The Default Symbols Visibility Case......Page 98
The Controlled Symbols Visibility Case......Page 99
Exporting the Windows Dynamic Library Symbols......Page 101
Using the __declspec(dllexport) Keyword......Page 102
Using the Module-definition File (.def)......Page 107
--no-undefined Linker Flag......Page 111
Runtime Dynamic Linking......Page 112
Dynamic Linking Modes Comparison......Page 115
Development Use Case Scenario......Page 116
Linux Static Library Naming Conventions......Page 117
Dynamic Library Soname......Page 118
Linux Build Time Library Location Rules Details......Page 119
Beginners’ Mistakes: What Can Possibly Go Wrong and How to Avoid It......Page 120
Windows Build Time Library Location Rules......Page 121
Project Linker Settings......Page 122
#pragma Comment......Page 123
Implicit Referencing of the Library Project......Page 124
Runtime Dynamic Library Location Rules......Page 127
rpath......Page 128
LD_LIBRARY_PATH Environment Variable......Page 129
ldconfig Cache......Page 130
Windows Runtime Dynamic Library Location Rules......Page 132
Linux Demo of Build Time and Runtime Conventions......Page 133
Why Resolved Memory Addresses Are a Must......Page 137
General Problem of Resolving References......Page 138
Scenario 1: Client Binary Needs to Know the Address of Dynamic Library Symbols......Page 140
Scenario 2: Loaded Library No Longer Knows the Addresses of Its Own Symbols......Page 141
Overall Strategy......Page 143
Tactics......Page 144
Linker Directives Overview......Page 146
Load Time Relocation (LTR)......Page 149
Position Independent Code (PIC)......Page 150
Rules and Limitations of the Recursive Chain of Dynamic Linking......Page 152
Strong Implementation Preferences......Page 153
Typical Duplicate Symbols Scenarios......Page 155
Duplicate C++ Symbols......Page 156
Duplicate Symbols Default Handling......Page 158
Duplicate Local Symbols Are Allowed......Page 160
Duplicate Symbols Handling When Linking in Dynamic Libraries......Page 161
Duplicate Symbols and Dynamic Linking Modes......Page 164
Third Level Priority (Unprioritized, Noncompeting) Symbols......Page 165
Case 1: Client Binary Symbol Collides with Dynamic Library ABI Function......Page 166
Case 2: ABI Symbols of Different Dynamic Libraries Collide......Page 170
Impact of Different Linking Order......Page 173
Case 3: Dynamic Library ABI Symbol Collides with Another Dynamic Library Local Symbol......Page 174
Case 4: Dynamic Library Non-exported Symbol Collides with Another Dynamic Library Non-exported Symbol......Page 176
Interesting Scenario: Singleton in Static Library......Page 180
Solving the Problem......Page 184
Final Remark: Linking Does Not Provide Any Kind of Namespace Inheritance......Page 185
Major Version Code Changes......Page 186
The Usual Dynamic Library Upgrade Practices......Page 187
Combining Softlink and Soname in the Library Upgrade Scheme......Page 188
Extra Softlink Needed as Convenience for Development Scenarios......Page 189
The Softlink’s Role......Page 190
Technicalities of the Soname Implementation......Page 191
Soname Propagated into the Client Binary File......Page 192
The Support from the Other Utility Programs (ldconfig)......Page 193
The Advantage of Symbol Versioning Mechanism......Page 194
Phase 1: Initial Version......Page 195
Phase 3: Major Version Changes......Page 196
Linker Version Script......Page 198
.symver Assembler Directive......Page 199
Sample Project Analysis: Phase 1 (Initial Version)......Page 200
ELF Format Support......Page 201
Propagation of Version Symbol Information to the Client Binaries......Page 203
Sample Project Analysis: Phase 2 (Minor Version Changes)......Page 204
The Case of Changed ABI Function Behavior......Page 209
The Case of Changed ABI Function Prototype......Page 213
Symbols Export Control......Page 215
Unnamed Node......Page 216
Windows Dynamic Libraries Versioning......Page 217
DLL Version Information......Page 218
Specifying DLL Version Information......Page 219
Querying and Retrieving DLL Version Information......Page 221
Linking Requirements......Page 222
Elegant Way: Calling the DLL’s DllGetVersion Function......Page 223
Brutal Alternative: Examining File Version Directly......Page 228
Plug-in Concept......Page 231
Popular Plug-in Architectures......Page 233
Runtime Quick Substitution Ability......Page 234
Converting Dynamic Library to Executable......Page 235
Conflicting Runtime Memory Handling of Windows Libraries......Page 238
Linker Weak Symbols Explained......Page 240
size Utility Program......Page 241
ldd......Page 242
Safer ldd Alternatives......Page 243
nm......Page 244
Listing and Examining Sections......Page 246
Listing All Symbols......Page 248
Examining Dynamic Section......Page 249
Examining Relocation Section......Page 250
Listing and Examining Segments......Page 251
Disassembling the Code......Page 252
Parsing ELF Header......Page 255
Listing and Examining Sections......Page 257
Listing All Symbols......Page 258
Examining the Dynamic Section......Page 260
Examining the Relocation Section......Page 261
Listing and Examining Segments......Page 262
Detecting the Debug Build......Page 263
Deployment Phase Tools......Page 264
chrpath......Page 265
patchelf......Page 266
strace......Page 267
addr2line......Page 268
gdb (GNU Debugger)......Page 269
ar......Page 271
Listing the Static Library Object Files......Page 272
Adding the New Object File to the Static Library......Page 273
Restoring the Order of Object Files......Page 274
Debugging the Linking......Page 275
Determining the Binary File Type......Page 276
Determining the Dynamic Library Entry Point......Page 277
List Symbols......Page 278
Examining Specific Sections......Page 279
Examining the Data Section......Page 280
Disassembling the Running Process......Page 281
Listing Dynamically Linked Libraries......Page 282
/proc//maps File......Page 283
lsof Utility......Page 284
Programmatic Way......Page 285
Creating and Maintaining the Static Library......Page 288
Library Manager (lib.exe)......Page 289
lib.exe as a Command Line Utility......Page 290
Creating a Static Library......Page 291
Removing Individual Object Files from the Static Library......Page 292
Extracting the Individual Object File from the Static Library......Page 293
lib.exe in the Realm of Dynamic Libraries (Import Library Tool)......Page 294
Identifying the Binary File Type......Page 296
Listing the DLL Exported Symbols......Page 297
Listing and Examining the Sections......Page 298
Disassembling the Code......Page 300
Identifying the Debug Build......Page 301
Object Files......Page 302
DLLs and Executables......Page 303
Listing the Load Time Dependencies......Page 304
Dependency Walker......Page 305
Index......Page 307