Annotation of wikisrc/symbol_versions.mdwn, revision 1.3

1.1       riastrad    1: [[!meta title="Symbol Versions in NetBSD Libraries"]]
                      2: 
                      3: NetBSD implements various standard C language interfaces such as the
                      4: [[!template id=man name="time" section="3"]] function in POSIX in
                      5: `libc`, which has a prototype like this:
                      6: 
                      7:     time_t time(time_t *);
                      8: 
                      9: However, between NetBSD 5 and NetBSD 6, the definition of the type
                     10: `time_t` in NetBSD changed on many architectures from 32-bit to 64-bit
                     11: to avoid the
                     12: [year 2038 problem](https://en.wikipedia.org/wiki/Year_2038_problem).
                     13: So programs compiled in NetBSD<=5 saw a declaration like
                     14: 
                     15:     int time(int *);
                     16: 
                     17: which on most architectures is 32-bit, while programs compiled in
                     18: NetBSD>=6 see a declaration like
                     19: 
                     20:     int64_t time(int64_t *);
                     21: 
                     22: These declarations are not compatible -- consider a program with a
                     23: fragment like:
                     24: 
                     25:     int before, after;
                     26: 
                     27:     time(&before);
                     28:     ...
                     29:     time(&after);
                     30: 
                     31: This would work in NetBSD<=5, but in NetBSD>=6, the calls to
                     32: [[!template id=man name="time" section="3"]] might overwrite adjacent
                     33: positions on the stack, or crash altogether because the argument is
                     34: misaligned.
                     35: 
                     36: Programs written and compiled on older versions of NetBSD are supposed
                     37: to continue to work -- with suitable emulators/compatNN packages and
                     38: compatNN.kmod modules or COMPAT_NN kernel options -- on newer versions
                     39: of NetBSD.
                     40: 
                     41: To make this work, NetBSD's `libc` provides _two_ symbols:
                     42: 
                     43: - `time`, which still implements the legacy prototype as before; and
                     44: - `__time50` (yes, this is not a typo for `__time60`), which implements
                     45:   the new 64-bit prototype.
                     46: 
                     47: The declaration in newer NetBSD
                     48: [time.h](https://nxr.netbsd.org/xref/src/include/time.h) is actually:
                     49: 
                     50:     time_t time(time_t *) __RENAME(__time50);
                     51: 
                     52: where `__RENAME(__time50)` is a macro expanding to `__asm("__time50")`,
                     53: which has the effect that the compiler will use the symbol `__time50`
                     54: for calls to the C function this declares.
                     55: Thus, old programs with calls to the symbol `time` using the 32-bit
                     56: prototype will continue to work, and new programs will be compiled to
                     57: call the symbol `__time50` using the 64-bit prototype.
                     58: ([Details on how the symbols are implemented in `libc`.](https://nxr.netbsd.org/xref/src/lib/libc/README))
                     59: 
1.2       riastrad   60: # [[!template id=man name="dlsym" section="3"]] and symbol interposition
1.1       riastrad   61: 
                     62: **Programs that use
                     63: [[!template id=man name="dlsym" section="3"]],
                     64: such as C foreign function interfaces in dynamic languages like Python,
                     65: need to know that if they want the legacy 32-bit time() function, they
                     66: must use the symbol `time`, and if they want the modern 64-bit time()
1.3     ! riastrad   67: function, they must use the symbol `__time50`.**
        !            68: 
        !            69: **Similarly, programs that use `LD_PRELOAD` (see
1.1       riastrad   70: [[!template id=man name="ld.elf_so" section="3"]])
                     71: to interpose their own definitions of symbols, such as
                     72: [torsocks](https://gitlab.torproject.org/legacy/trac/-/wikis/doc/torsocks),
                     73: must know to define `__time50` if they want to replace the new
                     74: semantics in new programs, or `time` if they want to replace the old
                     75: semantics in old programs.**
1.3     ! riastrad   76: 
1.1       riastrad   77: The same applies to many other standard C functions, such as
                     78: [[!template id=man name="clock_gettime" section="3"]]
                     79: (`__clock_gettime50`) and
                     80: [[!template id=man name="socket" section="3"]]
                     81: (`__socket30`), which have all had their prototypes or semantics
                     82: revised at some point.
                     83: 
                     84: Symbol interposition is very difficult to get right, and it is hard to
                     85: make programs that do it reliably.
                     86: On NetBSD, it should be reserved for certain standard library functions
                     87: like `malloc` and `free` (and `calloc` and everything else in that
                     88: family), and some system call stubs; except for the `__...50`
                     89: pseudo-versioned renames of public functions, you should not try to
                     90: interpose your own definition of any symbol beginning with `_`, which
                     91: is reserved to the implementation in C.
                     92: 
1.2       riastrad   93: # Appendix: ELF symbol versions
1.1       riastrad   94: 
                     95: The renaming scheme of `__time50` is informal -- any symbol can be
                     96: renamed the same way, and NetBSD uses it for some other purposes, such
                     97: as exposing a slightly different
                     98: [[!template id=man name="rename" section="2"]]
                     99: function via the symbol `__posix_rename` in programs that define
                    100: `_POSIX_C_SOURCE` but not `_NETBSD_SOURCE`.
                    101: 
                    102: The GNU ELF toolchain (gcc, ld, &c.) supports a formal concept of
                    103: ‘symbol versions’ with sections called `.gnu.version` (associating
                    104: versions with symbols), `.gnu.version_d` (versions defined in an
                    105: object), and `.gnu.version_n` (versions needed in an object).
                    106: As of 2020, NetBSD does not use ELF symbol versions (although the
                    107: linker and loader support them).
                    108: 
                    109: The semantics is:
                    110: 
                    111: - When creating a library, a version map may be specified like so:
                    112: 
1.3     ! riastrad  113:       NetBSD_BASE {
        !           114:               global:
        !           115:                       __time50;
        !           116:                       free;
        !           117:                       malloc;
        !           118:                       time;
        !           119:               local:
        !           120:                       *;
        !           121:       };
        !           122: 
        !           123:       NetBSD_6 {
        !           124:               global:
        !           125:                       time;
        !           126:       };
1.1       riastrad  127: 
                    128:   The library can specify what versioned symbol each definition in the
                    129:   library is exposed with:
                    130: 
1.3     ! riastrad  131:       __asm(".symver time_legacy,time@NetBSD_BASE");
        !           132:       int time_legacy(int *t) { ... }
1.1       riastrad  133: 
1.3     ! riastrad  134:       __asm(".symver time64,time@@NetBSD_6");         /* default version */
        !           135:       int64_t time64(int64_t *t) { ... }
1.1       riastrad  136: 
1.3     ! riastrad  137:       __asm(".symver __time50,__time50@NetBSD_BASE");
        !           138:       __typeof(time) __time50 __attribute__((__alias__("time64")));
1.1       riastrad  139: 
                    140:   Versions marked with `@@` are _default_ versions; versions marked
                    141:   with `@` are non-default.
                    142: 
                    143: - When running a program that was linked _without_ ELF symbol versions,
                    144:   from before the library had ELF symbol versions (like `libc` today),
                    145:   the first version in the map is used to resolve symbols:
                    146: 
                    147:   - Old programs calling the legacy `time` symbol will get
                    148:     `time@NetBSD_BASE`, which is defined via `time_legacy` above.
                    149: 
                    150:   - Programs calling `__time50` will get `__time50@NetBSD_BASE`, which
                    151:     is defined via `time64` above.
                    152: 
                    153: - When linking a program against a library with symbol versions, the
                    154:   linker will record what the default version was; when later running
                    155:   the program, the stored symbol version will be used.
                    156:   If there is no default version, and the program did not request a
                    157:   specific version with `.symver`, then the linker refuses to link, so
                    158:   obsolete symbols can be ‘removed’ by giving them only non-default
                    159:   versions -- thus old programs continue to work but new programs can't
                    160:   be made that use the obsolete symbols.
                    161: 
                    162:   For example, if [[!template id=man name="time" section="3"]] is
                    163:   declared in a header file as simply
                    164: 
1.3     ! riastrad  165:       typedef int64_t time_t;
        !           166:       time_t time(time_t *);
1.1       riastrad  167: 
                    168:   then new programs will be linked against `time@NetBSD_6`, which is
                    169:   the default version for the symbol name `time`.
                    170:   If NetBSD ever changed the prototype of
                    171:   [[!template id=man name="time" section="3"]]
                    172:   again, and defined a `time@NetBSD_11` as the new default version,
                    173:   existing programs compiled with `time@NetBSD_6` would continue to get
                    174:   the semantics they were built against.
                    175: 
                    176: - When a program uses
                    177:   [[!template id=man name="dlsym" section="3"]],
                    178:   it always gets the default version, if any.
                    179:   Programs can request specific versions with
                    180:   [[!template id=man name="dlvsym" section="3"]].
                    181: 
1.2       riastrad  182: ## ELF symbol versions versus `__...50` pseudo-versions
1.1       riastrad  183: 
                    184: ELF symbol versions and NetBSD's `__time50` pseudo-version renaming
                    185: scheme both try to address the same problem: making sure old programs
                    186: that were built under the assumption of the old semantics continue to
                    187: run unmodified with new libraries.
                    188: 
                    189: Both of them run into problems with
                    190: [[!template id=man name="dlsym" section="3"]]
                    191: and symbol interposition:
                    192: 
                    193: - A program written _today_ that expects to find the function time() in
                    194:   `libc`, such as a C foreign function interface for a dynamic language
                    195:   like Python, needs to know to call `dlsym("__time50")`; otherwise it
                    196:   will get an obsolete definition that does not match the semantics of
                    197:   the current definition of `time_t`, possibly leading to data
                    198:   corruption, crashes, or worse.
                    199: 
                    200: - If `libc` used used ELF symbol versions, then `dlsym("time")` would
                    201:   return the modern symbol.
                    202: 
                    203:   But any _old_ programs that used `dlsym("time")` assuming it
                    204:   returned the legacy definition (which was the ‘modern’ definition at
                    205:   the time the programs were written and built) will break if it
                    206:   instead returns the 64-bit definition.
                    207: 
                    208:   And if we ever modified
                    209:   [[!template id=man name="time" section="3"]]
                    210:   again (hypothetically, to extend it to 128-bit galactic-scale times),
                    211:   programs written assuming that `dlsym("time")` returns the 64-bit
                    212:   definition will break if it begins to return the 128-bit definition.
                    213:   Programs could future-proof themselves by using `dlsym("time",
                    214:   "NetBSD_6")` explicitly, but this is no better than writing
                    215:   `dlsym("__time50")` explicitly.
                    216: 
                    217: **Thus, switching from the pseudo-versions we use to ELF symbol
                    218: versions doesn't improve the
                    219: [[!template id=man name="dlsym" section="3"]]
                    220: situation -- in fact, it makes the situation _worse_, by breaking old
                    221: programs and providing no way for new programs to bind to the name of
                    222: the current version.**
                    223: 
                    224: Perhaps we could create a compiler builtin `__builtin_asm_name` which
                    225: would expand to the `__asm("...")` name by which a C identifier has
                    226: been declared -- then programs could instead do:
                    227: 
                    228:     __typeof(time) *timep = dlsym(dso, __builtin_asm_name(time));
                    229: 
                    230: This way the text of the program is the same no matter how
                    231: [[!template id=man name="time" section="3"]]
                    232: is declared in the header file, but it will continue to work across
                    233: changes to the signature of the
                    234: [[!template id=man name="time" section="3"]]
                    235: function in newer releases of NetBSD.
                    236: 
1.2       riastrad  237: # References
1.1       riastrad  238: 
                    239: - Jörg Sonnenberger, ‘How to break long-term compatibility in NetBSD’,
                    240:   AsiaBSDcon 2016.
                    241:   https://www.NetBSD.org/gallery/presentations/joerg/asiabsdcon2016/asiabsdcon2016.pdf
                    242: 
                    243: - Ulrich Drepper, ‘How To Write Shared Libraries’, 2011-12-10.
                    244:   https://akkadia.org/drepper/dsohowto.pdf
                    245: 
                    246: - Ulrich Drepper, ‘ELF Symbol Versioning’.
                    247:   https://akkadia.org/drepper/symbol-versioning

CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb