![Page 1: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/1.jpg)
At the core of the user experience.®
Prelinker Usage for MIPS® Cores CELF Jamboree #11
Tokyo, Japan, Oct 27, 2006
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
Arvind Kumar, MIPS Technologies
Kazu Hirata, CodeSourcery
![Page 2: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/2.jpg)
2
Outline
What is the prelinker?
Object formats, loaders & issues
Driving the prelinker
Under the hood
Speedup
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 3: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/3.jpg)
3
Big Picture
Prelinker is a tool to make programs start faster
Processes libraries & executable A new step in system building
Best when whole system is prelinked
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 4: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/4.jpg)
4
New Features
Supports MIPS Linker, prelinker & dynamic linker changes
Works in cross environment Original prelinker only ran on the system being prelinked Modified to allow prelinking of a cross built root file system No longer need a live target system
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 5: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/5.jpg)
5
ELF Shared Libraries
Unspecified load address requires relocations
Symbolic resolution requires symbol table management
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 6: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/6.jpg)
6
Dynamic Loader
Allocate space in process's address space
Load library
Relocate the Global Offset Table
Apply relocations from .rel.dyn
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 7: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/7.jpg)
7
a.out shared libraries
Allocate address at link time Requires universal table of library addresses
No dynamic relocations No comdat or weak symbols No overriding by executable
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 8: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/8.jpg)
8
Issues
Symbol resolution takes time
Relocation causes pages to be unshared GOT is conceptually constant
Many more shared libraries used now
Global table of library addresses is unmanageable
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 9: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/9.jpg)
9
Prelinker
The flexibility of ELF
The speed of a.out
Fails gracefully
Postprocesses linked executables & libraries Preprocesses dynamic loaded images
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved. © 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved. © 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved. © 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved. © 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 10: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/10.jpg)
10
Modes of Operation
Prelink all the libraries & executables in these directories
Prelink this set of libraries to be coresident
Prelink this executable against these libraries
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 11: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/11.jpg)
11
Dynamic Loader
Checks consistency of dependent libraries ensures none have changed since executable was prelinked
If all is well then loads the libraries & executables without needing dynamic linking
Otherwise falls back to old behaviour Graceful degradation
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 12: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/12.jpg)
12
Limitations
Cannot automatically prelink dlopen'ed libraries can manually prelink them
Requires updated linker and dynamic loader Libraries produced by older tools will force full linking
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 13: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/13.jpg)
13
Operation
Scans libraries and executables
Builds dependency tree
Determines which libraries are coresident
Allocates base addresses
Prelinks the libraries & executables
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 14: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/14.jpg)
14
Prelinking Libraries
Adjust every local GOT entry
Resolve global GOT entries Some entries cannot be prelinked
Apply relocations according to assigned base addresses Relocations to other libraries Relocations within library
Insert some ident information
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 15: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/15.jpg)
15
Prelinking Executables Apply relocations
Relocations to libraries
Resolve global GOT entries for externaly defined symbols
Insert ident information
Create conflict list Executable can override symbols in libraries Global GOT entries that couldn't be prelinked TLS relocations
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 16: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/16.jpg)
16
Ident
DT_GNU_PRELINKED provides a timestamp
DT_CHECKSUM provides a checksum
.gnu.liblist list of libraries needed
.gnu.libstr names of libraries
dynamic loader checks each library's DT_GNU_PRELINKED & DT_CHECKSUM against .gnu.liblist
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 17: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/17.jpg)
17
Conversion to RELA Sometimes SHT_REL must be converted to SHT_RELA
Need correct addend when reprelinking the library or when prelink checks fail Some other (changed) library may override or stop overriding the base symbol So we can't 'undo' the REL relocation to extract the addend We must explicitly store the addend somewhere Or use a relocation type that has no addend
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 18: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/18.jpg)
18
Conflicts An executable can override a symbol in a shared library
Must update shared library when loaded Prelinker generates such a list for each executable Places it in .gnu.conflicts section Dynamic loader iterates over the list Each conflict causes that page to be duplicated (copy on write) If there are no conflicts, the are no page copies
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 19: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/19.jpg)
19
Conflict Causes Improperly linked shared libraries
prelinker will warn
Library (or executable) overriding a symbol eg libpthread.so overrides libc.so
COMDAT symbol occurs in multiple places, must pick one
Library's GOT can point to a lazy binding stub
Thread Local Storage
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 20: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/20.jpg)
20
Conflict Optimizations
C++ code can have many COMDAT instances
Some are generated by compiler vtable, rtti info
These do not have to be commonized
Prelinker will not emit conflicts for these
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 21: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/21.jpg)
21
Undo
Prelinker has an undo operation
.gnu.prelink_undo section contains original ELF, program & section headers
Can unprelink specific executables & libraries or unprelink everything found in a search path
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 22: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/22.jpg)
22
Size Changes
Prelinking increases the on-disk image undo info, REL->RELA conversion, conflict list
A test system of 422 executables and 38 libraries went from 86.8MB to 88.5MB (2%)
For libc only, 1749K to 1752K (0.2%) For libstdc++ only, 1413K to 1426K (0.9%) libc avoided the REL->RELA conversion, but libstdc++ did not.
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 23: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/23.jpg)
23
Speedup
Speedup is noticeable in two places large gui applications with lots of shared libraries shell scripts rapidly invoking many worker tools
Test machine MIPS64® 20Kc™ core @ 500MHz
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 24: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/24.jpg)
24
Speedup
Test machine MIPS64® 20Kc™ core @500MHz
/bin/bash -c /bin/true (300 iterations) 7.4s to 4.6s ~38% improvement
/bin/bash -c /bin/ls >/dev/null (300 iterations) 10.5s to 7.0s ~33% improvement
firefox (100 iterations, no X server) 30.9 to 16.7 ~45% improvement
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 25: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/25.jpg)
25
Speedup
Test machine MIPS32® 24Kc™ core @266MHz
/bin/bash -c /bin/true (300 iterations) 8.9s to 4.9s ~45% improvement
/bin/bash -c /bin/ls >/dev/null (300 iterations) 12.3s to 7.2s ~42% improvement
firefox (100 iterations, no X server) 43.9 to 24.4 ~44% improvement
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.
![Page 26: Prelinker Usage for MIPS® Cores CELF Jamboree #11 Tokyo ... · 18 Conflicts ÎAn executable can override a symbol in a shared library Î Must update shared library when loaded Î](https://reader034.vdocuments.us/reader034/viewer/2022042102/5e7e97965a2a97503e181291/html5/thumbnails/26.jpg)
26
Where are the Patches?
Binutils FSF mainline in progress sourceware.org
GLIBC FSF mainline in progress sourceware.org/glibc
Prelinker in progress svn://sourceware.org/svn/prelink
None are yet in official releases
© 2006 CodeSourcery, Inc. & MIPS Technologies, Inc. All rights reserved.