Annotation of wikisrc/symbol_versions.mdwn, revision 1.4

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

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