11 files changed, 426 insertions(+), 462 deletions(-)
A COPYING
A Makefile
A README.md
D linux/README
D linux/env.ha
D linux/keyctl/README
D linux/keyctl/keyctl.ha
D linux/keyctl/types.ha
D linux/start+libc.ha
D linux/start.ha
D linux/vdso/vdso.ha
A COPYING => COPYING +367 -0
@@ 0,0 1,367 @@
+Mozilla Public License Version 2.0
+==================================
+
+1. Definitions
+--------------
+
+1.1. "Contributor"
+ means each individual or legal entity that creates, contributes to
+ the creation of, or owns Covered Software.
+
+1.2. "Contributor Version"
+ means the combination of the Contributions of others (if any) used
+ by a Contributor and that particular Contributor's Contribution.
+
+1.3. "Contribution"
+ means Covered Software of a particular Contributor.
+
+1.4. "Covered Software"
+ means Source Code Form to which the initial Contributor has attached
+ the notice in Exhibit A, the Executable Form of such Source Code
+ Form, and Modifications of such Source Code Form, in each case
+ including portions thereof.
+
+1.5. "Incompatible With Secondary Licenses"
+ means
+
+ (a) that the initial Contributor has attached the notice described
+ in Exhibit B to the Covered Software; or
+
+ (b) that the Covered Software was made available under the terms of
+ version 1.1 or earlier of the License, but not also under the
+ terms of a Secondary License.
+
+1.6. "Executable Form"
+ means any form of the work other than Source Code Form.
+
+1.7. "Larger Work"
+ means a work that combines Covered Software with other material, in
+ a separate file or files, that is not Covered Software.
+
+1.8. "License"
+ means this document.
+
+1.9. "Licensable"
+ means having the right to grant, to the maximum extent possible,
+ whether at the time of the initial grant or subsequently, any and
+ all of the rights conveyed by this License.
+
+1.10. "Modifications"
+ means any of the following:
+
+ (a) any file in Source Code Form that results from an addition to,
+ deletion from, or modification of the contents of Covered
+ Software; or
+
+ (b) any new file in Source Code Form that contains any Covered
+ Software.
+
+1.11. "Patent Claims" of a Contributor
+ means any patent claim(s), including without limitation, method,
+ process, and apparatus claims, in any patent Licensable by such
+ Contributor that would be infringed, but for the grant of the
+ License, by the making, using, selling, offering for sale, having
+ made, import, or transfer of either its Contributions or its
+ Contributor Version.
+
+1.12. "Secondary License"
+ means either the GNU General Public License, Version 2.0, the GNU
+ Lesser General Public License, Version 2.1, the GNU Affero General
+ Public License, Version 3.0, or any later versions of those
+ licenses.
+
+1.13. "Source Code Form"
+ means the form of the work preferred for making modifications.
+
+1.14. "You" (or "Your")
+ means an individual or a legal entity exercising rights under this
+ License. For legal entities, "You" includes any entity that
+ controls, is controlled by, or is under common control with You. For
+ purposes of this definition, "control" means (a) the power, direct
+ or indirect, to cause the direction or management of such entity,
+ whether by contract or otherwise, or (b) ownership of more than
+ fifty percent (50%) of the outstanding shares or beneficial
+ ownership of such entity.
+
+2. License Grants and Conditions
+--------------------------------
+
+2.1. Grants
+
+Each Contributor hereby grants You a world-wide, royalty-free,
+non-exclusive license:
+
+(a) under intellectual property rights (other than patent or trademark)
+ Licensable by such Contributor to use, reproduce, make available,
+ modify, display, perform, distribute, and otherwise exploit its
+ Contributions, either on an unmodified basis, with Modifications, or
+ as part of a Larger Work; and
+
+(b) under Patent Claims of such Contributor to make, use, sell, offer
+ for sale, have made, import, and otherwise transfer either its
+ Contributions or its Contributor Version.
+
+2.2. Effective Date
+
+The licenses granted in Section 2.1 with respect to any Contribution
+become effective for each Contribution on the date the Contributor first
+distributes such Contribution.
+
+2.3. Limitations on Grant Scope
+
+The licenses granted in this Section 2 are the only rights granted under
+this License. No additional rights or licenses will be implied from the
+distribution or licensing of Covered Software under this License.
+Notwithstanding Section 2.1(b) above, no patent license is granted by a
+Contributor:
+
+(a) for any code that a Contributor has removed from Covered Software;
+ or
+
+(b) for infringements caused by: (i) Your and any other third party's
+ modifications of Covered Software, or (ii) the combination of its
+ Contributions with other software (except as part of its Contributor
+ Version); or
+
+(c) under Patent Claims infringed by Covered Software in the absence of
+ its Contributions.
+
+This License does not grant any rights in the trademarks, service marks,
+or logos of any Contributor (except as may be necessary to comply with
+the notice requirements in Section 3.4).
+
+2.4. Subsequent Licenses
+
+No Contributor makes additional grants as a result of Your choice to
+distribute the Covered Software under a subsequent version of this
+License (see Section 10.2) or under the terms of a Secondary License (if
+permitted under the terms of Section 3.3).
+
+2.5. Representation
+
+Each Contributor represents that the Contributor believes its
+Contributions are its original creation(s) or it has sufficient rights
+to grant the rights to its Contributions conveyed by this License.
+
+2.6. Fair Use
+
+This License is not intended to limit any rights You have under
+applicable copyright doctrines of fair use, fair dealing, or other
+equivalents.
+
+2.7. Conditions
+
+Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
+in Section 2.1.
+
+3. Responsibilities
+-------------------
+
+3.1. Distribution of Source Form
+
+All distribution of Covered Software in Source Code Form, including any
+Modifications that You create or to which You contribute, must be under
+the terms of this License. You must inform recipients that the Source
+Code Form of the Covered Software is governed by the terms of this
+License, and how they can obtain a copy of this License. You may not
+attempt to alter or restrict the recipients' rights in the Source Code
+Form.
+
+3.2. Distribution of Executable Form
+
+If You distribute Covered Software in Executable Form then:
+
+(a) such Covered Software must also be made available in Source Code
+ Form, as described in Section 3.1, and You must inform recipients of
+ the Executable Form how they can obtain a copy of such Source Code
+ Form by reasonable means in a timely manner, at a charge no more
+ than the cost of distribution to the recipient; and
+
+(b) You may distribute such Executable Form under the terms of this
+ License, or sublicense it under different terms, provided that the
+ license for the Executable Form does not attempt to limit or alter
+ the recipients' rights in the Source Code Form under this License.
+
+3.3. Distribution of a Larger Work
+
+You may create and distribute a Larger Work under terms of Your choice,
+provided that You also comply with the requirements of this License for
+the Covered Software. If the Larger Work is a combination of Covered
+Software with a work governed by one or more Secondary Licenses, and the
+Covered Software is not Incompatible With Secondary Licenses, this
+License permits You to additionally distribute such Covered Software
+under the terms of such Secondary License(s), so that the recipient of
+the Larger Work may, at their option, further distribute the Covered
+Software under the terms of either this License or such Secondary
+License(s).
+
+3.4. Notices
+
+You may not remove or alter the substance of any license notices
+(including copyright notices, patent notices, disclaimers of warranty,
+or limitations of liability) contained within the Source Code Form of
+the Covered Software, except that You may alter any license notices to
+the extent required to remedy known factual inaccuracies.
+
+3.5. Application of Additional Terms
+
+You may choose to offer, and to charge a fee for, warranty, support,
+indemnity or liability obligations to one or more recipients of Covered
+Software. However, You may do so only on Your own behalf, and not on
+behalf of any Contributor. You must make it absolutely clear that any
+such warranty, support, indemnity, or liability obligation is offered by
+You alone, and You hereby agree to indemnify every Contributor for any
+liability incurred by such Contributor as a result of warranty, support,
+indemnity or liability terms You offer. You may include additional
+disclaimers of warranty and limitations of liability specific to any
+jurisdiction.
+
+4. Inability to Comply Due to Statute or Regulation
+---------------------------------------------------
+
+If it is impossible for You to comply with any of the terms of this
+License with respect to some or all of the Covered Software due to
+statute, judicial order, or regulation then You must: (a) comply with
+the terms of this License to the maximum extent possible; and (b)
+describe the limitations and the code they affect. Such description must
+be placed in a text file included with all distributions of the Covered
+Software under this License. Except to the extent prohibited by statute
+or regulation, such description must be sufficiently detailed for a
+recipient of ordinary skill to be able to understand it.
+
+5. Termination
+--------------
+
+5.1. The rights granted under this License will terminate automatically
+if You fail to comply with any of its terms. However, if You become
+compliant, then the rights granted under this License from a particular
+Contributor are reinstated (a) provisionally, unless and until such
+Contributor explicitly and finally terminates Your grants, and (b) on an
+ongoing basis, if such Contributor fails to notify You of the
+non-compliance by some reasonable means prior to 60 days after You have
+come back into compliance. Moreover, Your grants from a particular
+Contributor are reinstated on an ongoing basis if such Contributor
+notifies You of the non-compliance by some reasonable means, this is the
+first time You have received notice of non-compliance with this License
+from such Contributor, and You become compliant prior to 30 days after
+Your receipt of the notice.
+
+5.2. If You initiate litigation against any entity by asserting a patent
+infringement claim (excluding declaratory judgment actions,
+counter-claims, and cross-claims) alleging that a Contributor Version
+directly or indirectly infringes any patent, then the rights granted to
+You by any and all Contributors for the Covered Software under Section
+2.1 of this License shall terminate.
+
+5.3. In the event of termination under Sections 5.1 or 5.2 above, all
+end user license agreements (excluding distributors and resellers) which
+have been validly granted by You or Your distributors under this License
+prior to termination shall survive termination.
+
+************************************************************************
+* *
+* 6. Disclaimer of Warranty *
+* ------------------------- *
+* *
+* Covered Software is provided under this License on an "as is" *
+* basis, without warranty of any kind, either expressed, implied, or *
+* statutory, including, without limitation, warranties that the *
+* Covered Software is free of defects, merchantable, fit for a *
+* particular purpose or non-infringing. The entire risk as to the *
+* quality and performance of the Covered Software is with You. *
+* Should any Covered Software prove defective in any respect, You *
+* (not any Contributor) assume the cost of any necessary servicing, *
+* repair, or correction. This disclaimer of warranty constitutes an *
+* essential part of this License. No use of any Covered Software is *
+* authorized under this License except under this disclaimer. *
+* *
+************************************************************************
+
+************************************************************************
+* *
+* 7. Limitation of Liability *
+* -------------------------- *
+* *
+* Under no circumstances and under no legal theory, whether tort *
+* (including negligence), contract, or otherwise, shall any *
+* Contributor, or anyone who distributes Covered Software as *
+* permitted above, be liable to You for any direct, indirect, *
+* special, incidental, or consequential damages of any character *
+* including, without limitation, damages for lost profits, loss of *
+* goodwill, work stoppage, computer failure or malfunction, or any *
+* and all other commercial damages or losses, even if such party *
+* shall have been informed of the possibility of such damages. This *
+* limitation of liability shall not apply to liability for death or *
+* personal injury resulting from such party's negligence to the *
+* extent applicable law prohibits such limitation. Some *
+* jurisdictions do not allow the exclusion or limitation of *
+* incidental or consequential damages, so this exclusion and *
+* limitation may not apply to You. *
+* *
+************************************************************************
+
+8. Litigation
+-------------
+
+Any litigation relating to this License may be brought only in the
+courts of a jurisdiction where the defendant maintains its principal
+place of business and such litigation shall be governed by laws of that
+jurisdiction, without reference to its conflict-of-law provisions.
+Nothing in this Section shall prevent a party's ability to bring
+cross-claims or counter-claims.
+
+9. Miscellaneous
+----------------
+
+This License represents the complete agreement concerning the subject
+matter hereof. If any provision of this License is held to be
+unenforceable, such provision shall be reformed only to the extent
+necessary to make it enforceable. Any law or regulation which provides
+that the language of a contract shall be construed against the drafter
+shall not be used to construe this License against a Contributor.
+
+10. Versions of the License
+---------------------------
+
+10.1. New Versions
+
+Mozilla Foundation is the license steward. Except as provided in Section
+10.3, no one other than the license steward has the right to modify or
+publish new versions of this License. Each version will be given a
+distinguishing version number.
+
+10.2. Effect of New Versions
+
+You may distribute the Covered Software under the terms of the version
+of the License under which You originally received the Covered Software,
+or under the terms of any subsequent version published by the license
+steward.
+
+10.3. Modified Versions
+
+If you create software not governed by this License, and you want to
+create a new license for such software, you may create and use a
+modified version of this License if you rename the license and remove
+any references to the name of the license steward (except to note that
+such modified license differs from this License).
+
+10.4. Distributing Source Code Form that is Incompatible With Secondary
+Licenses
+
+If You choose to distribute Source Code Form that is Incompatible With
+Secondary Licenses under the terms of this version of the License, the
+notice described in Exhibit B of this License must be attached.
+
+Exhibit A - Source Code Form License Notice
+-------------------------------------------
+
+ This Source Code Form is subject to the terms of the Mozilla Public
+ License, v. 2.0. If a copy of the MPL was not distributed with this
+ file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+If it is not possible or desirable to put the notice in a particular
+file, then You may include the notice in a location (such as a LICENSE
+file in a relevant directory) where a recipient would be likely to look
+for such a notice.
+
+You may add additional accurate notices of copyright ownership.
A Makefile => Makefile +35 -0
@@ 0,0 1,35 @@
+.POSIX:
+.SUFFIXES:
+HARE=hare
+HAREFLAGS=
+HAREDOC=haredoc
+
+DESTDIR=
+PREFIX=/usr/local
+SRCDIR=$(PREFIX)/src
+HARESRCDIR=$(SRCDIR)/hare
+THIRDPARTYDIR=$(HARESRCDIR)/third-party
+
+all:
+ @true # no-op
+
+check:
+ hare test
+
+docs:
+ mkdir -p docs/image/png
+ [ -e docs/mascot.jpg ] || curl -s https://harelang.org/mascot.jpg > docs/mascot.jpg
+ haredoc -Fhtml image::png > docs/iobus/index.html
+
+clean:
+ rm -rf docs
+
+install:
+ mkdir -p "$(DESTDIR)$(THIRDPARTYDIR)"/linux
+ mkdir -p "$(DESTDIR)$(THIRDPARTYDIR)"/linux/io_uring
+ install -m644 linux/io_uring/*.ha "$(DESTDIR)$(THIRDPARTYDIR)"/linux/io_uring
+
+uninstall:
+ rm -rf $(DESTDIR)$(THIRDPARTYDIR)/iobus
+
+.PHONY: all docs clean check install uninstall
A README.md => README.md +24 -0
@@ 0,0 1,24 @@
+# hare-linux
+
+This package expands upon the upstream linux module to add support for more
+Linux-specific interfaces.
+
+## Installation
+
+### From your distribution
+
+The recommended name for this package is "hare-linux". Look for this, or
+something similar, in your local package manager. This is the preferred way to
+install this package.
+
+### System-wide installation
+
+```
+make install
+```
+
+### Vendoring
+
+```
+git subtree -P vendor/hare-linux/ add https://git.sr.ht/~sircmpwn/hare-linux master
+```
D linux/README => linux/README +0 -1
@@ 1,1 0,0 @@
-The linux module provides support for Linux-specific platform functionality.
D linux/env.ha => linux/env.ha +0 -6
@@ 1,6 0,0 @@
-// License: MPL-2.0
-// (c) 2021 Mykyta Holubakha <hilobakho@gmail.com>
-use format::elf;
-
-// System V auxillary vector for the current process
-export let auxv: *[*]format::elf::auxv64 = null: *[*]format::elf::auxv64;
D linux/keyctl/README => linux/keyctl/README +0 -3
@@ 1,3 0,0 @@
-linux::keyctl provides an interface to the Linux kernel's key management
-facilities. Documentation for this module is sparse; the reader is encouraged to
-consult the Linux man pages for complete details.
D linux/keyctl/keyctl.ha => linux/keyctl/keyctl.ha +0 -102
@@ 1,102 0,0 @@
-use bytes;
-use errors;
-use rt;
-use strings;
-
-fn errno(errno: rt::errno) error = {
- switch (errno) {
- case rt::ENOKEY =>
- return nokey;
- case =>
- return errors::errno(errno);
- };
-};
-
-// Adds a key to the kernel's key management facility.
-export fn add_key(
- keytype: str,
- name: str,
- payload: []u8,
- keyring: serial,
-) (serial | error) = {
- const keytype = strings::to_c(keytype);
- defer free(keytype);
- const name = strings::to_c(name);
- defer free(name);
- match (rt::add_key(keytype, name, payload: *[*]u8: *void,
- len(payload), keyring)) {
- case let err: rt::errno =>
- return errno(err);
- case let n: int =>
- return n: serial;
- };
-};
-
-fn keyctl(
- cmd: command,
- arg2: u64,
- arg3: u64,
- arg4: u64,
- arg5: u64,
-) (int | error) = {
- match (rt::keyctl(cmd, arg2, arg3, arg4, arg5)) {
- case let err: rt::errno =>
- return errno(err);
- case let n: int =>
- return n;
- };
-};
-
-// Maps a special key or keyring ID to the serial number of the key actually
-// representing that feature. If it does not exist and 'create' is true, then
-// the key or keyring will be created if it is appropriate to do so.
-export fn get_keyring_id(key: serial, create: bool) (serial | error) = {
- return keyctl(command::GET_KEYRING_ID,
- key: u64, if (create) 1 else 0, 0, 0)?: serial;
-};
-
-// Replace the session keyring this process subscribes to with a new session
-// keyring using the given name, or, given an empty string, "_ses".
-export fn join_session_keyring(name: str) (serial | error) = {
- let name = if (name == "") {
- yield null;
- } else {
- yield strings::to_c(name);
- };
- defer free(name);
- return keyctl(command::JOIN_SESSION_KEYRING,
- name: uintptr: u64, 0, 0, 0)?: serial;
-};
-
-// Update a key's payload.
-export fn update(id: serial, payload: []u8) (void | error) = {
- keyctl(command::UPDATE, id: u64,
- payload: *[*]u8: uintptr: u64,
- len(payload): u64, 0)?;
-};
-
-// Revoke the key with the provided ID.
-export fn revoke(id: serial) (void | error) = {
- keyctl(command::REVOKE, id: u64, 0, 0, 0)?;
-};
-
-// Reads the payload from a key, returning the size of the key data. The
-// provided buffer may be empty to probe the key size without reading.
-export fn read(id: serial, buf: []u8) (size | error) = {
- const bufln = len(buf);
- const buf = if (len(buf) == 0) {
- yield null;
- } else {
- yield buf: *[*]u8: *void;
- };
- return keyctl(command::READ, id: u64,
- buf: uintptr: u64, bufln: u64, 0)?: size;
-};
-
-@test fn keyctl() void = {
- const payload = strings::toutf8("hello world");
- const key = add_key("user", "example", payload, PROCESS_KEYRING)!;
- let buffer: [64]u8 = [0...];
- const n = read(key, buffer[..])!;
- assert(bytes::equal(buffer[..n], payload));
-};
D linux/keyctl/types.ha => linux/keyctl/types.ha +0 -139
@@ 1,139 0,0 @@
-use errors;
-
-// A key ID.
-export type serial = i32;
-
-// Returned when a desired key was not found.
-export type nokey = !void;
-
-export type error = !(nokey | errors::error);
-
-// The caller's thread-specific keyring.
-export def THREAD_KEYRING: serial = -1;
-
-// The caller's process-specific keyring.
-export def PROCESS_KEYRING: serial = -2;
-
-// The caller's session-specific keyring.
-export def SESSION_KEYRING: serial = -3;
-
-// The caller's UID-specific keyring.
-export def USER_KEYRING: serial = -4;
-
-// The caller's UID-session keyring.
-export def USER_SESSION_KEYRING: serial = -5;
-
-// The caller's GID-specific keyring.
-export def GROUP_KEYRING: serial = -6;
-
-// The caller's GID-session keyring.
-export def REQKEY_AUTH_KEY: serial = -7;
-
-// The Key ID for the [[request_key]] destination keyring.
-export def REQUESTOR_KEYRING: serial = -8;
-
-// request-key default keyrings
-export type reqkey = enum int {
- NO_CHANGE = -1,
- DEFAULT = 0,
- THREAD_KEYRING = 1,
- PROCESS_KEYRING = 2,
- SESSION_KEYRING = 3,
- USER_KEYRING = 4,
- USER_SESSION_KEYRING = 5,
- GROUP_KEYRING = 6,
- REQUESTOR_KEYRING = 7,
-};
-
-// keyctl commands
-export type command = enum int {
- GET_KEYRING_ID = 0,
- JOIN_SESSION_KEYRING = 1,
- UPDATE = 2,
- REVOKE = 3,
- CHOWN = 4,
- SETPERM = 5,
- DESCRIBE = 6,
- CLEAR = 7,
- LINK = 8,
- UNLINK = 9,
- SEARCH = 10,
- READ = 11,
- INSTANTIATE = 12,
- NEGATE = 13,
- SET_REQKEY_KEYRING = 14,
- SET_TIMEOUT = 15,
- ASSUME_AUTHORITY = 16,
- GET_SECURITY = 17,
- SESSION_TO_PARENT = 18,
- REJECT = 19,
- INSTANTIATE_IOV = 20,
- INVALIDATE = 21,
- GET_PERSISTENT = 22,
- DH_COMPUTE = 23,
- PKEY_QUERY = 24,
- PKEY_ENCRYPT = 25,
- PKEY_DECRYPT = 26,
- PKEY_SIGN = 27,
- PKEY_VERIFY = 28,
- RESTRICT_KEYRING = 29,
- MOVE = 30,
- CAPABILITIES = 31,
- WATCH_KEY = 32,
-};
-
-// Input for [[command::DH_COMPUTE]]
-export type dh_params = struct {
- private: i32,
- prime: i32,
- base: i32,
-};
-
-// Output for [[command::DH_COMPUTE]]
-export type kdf_params = struct {
- hashname: *char,
- otherinfo: *char,
- otherinfolen: u32,
- __spare: [8]u32,
-};
-
-export type support = enum u32 {
- SUPPORTS_ENCRYPT = 0x01,
- SUPPORTS_DECRYPT = 0x02,
- SUPPORTS_SIGN = 0x04,
- SUPPORTS_VERIFY = 0x08,
-};
-
-export type pkey_query = struct {
- supported_ops: u32,
- key_size: u32,
- max_data_size: u16,
- max_sig_size: u16,
- max_enc_size: u16,
- max_dec_size: u16,
- __spare: [10]u32,
-};
-
-export type pkey_params = struct {
- key_id: i32,
- in_len: u32,
- union {
- out_len: u32,
- in2_len: u32,
- },
- __spare: [7]u32,
-};
-
-export type caps = enum u8 {
- CAPS0_CAPABILITIES = 0x01,
- CAPS0_PERSISTENT_KEYRINGS = 0x02,
- CAPS0_DIFFIE_HELLMAN = 0x04,
- CAPS0_PUBLIC_KEY = 0x08,
- CAPS0_BIG_KEY = 0x10,
- CAPS0_INVALIDATE = 0x20,
- CAPS0_RESTRICT_KEYRING = 0x40,
- CAPS0_MOVE = 0x80,
- CAPS1_NS_KEYRING_NAME = 0x01,
- CAPS1_NS_KEY_TAG = 0x02,
- CAPS1_NOTIFICATIONS = 0x04,
-};
D linux/start+libc.ha => linux/start+libc.ha +0 -14
@@ 1,14 0,0 @@
-// License: MPL-2.0
-// (c) 2022 Alexey Yerin <yyp@disroot.org>
-use rt;
-use format::elf;
-
-@init fn init_linux() void = {
- rt::start_linux();
-
- let i = 0;
- for (rt::envp[i] != null) {
- i += 1;
- };
- auxv = &rt::envp[i + 1]: *[*]elf::auxv64;
-};
D linux/start.ha => linux/start.ha +0 -12
@@ 1,12 0,0 @@
-// License: MPL-2.0
-// (c) 2021 Mykyta Holubakha <hilobakho@gmail.com>
-use rt;
-use format::elf;
-
-@init fn init_linux() void = {
- let i = 0;
- for (rt::envp[i] != null) {
- i += 1;
- };
- auxv = &rt::envp[i + 1]: *[*]elf::auxv64;
-};
D linux/vdso/vdso.ha => linux/vdso/vdso.ha +0 -185
@@ 1,185 0,0 @@
-// License: MPL-2.0
-// (c) 2021 Drew DeVault <sir@cmpwn.com>
-// (c) 2021 Eyal Sawady <ecs@d2evs.net>
-// (c) 2021 Mykyta Holubakha <hilobakho@gmail.com>
-use strings;
-use format::elf;
-use linux;
-
-let ehdr: nullable *elf::header64 = null;
-
-fn sys_infoehdr() nullable *elf::header64 = {
- static let ehdr_checked = false;
- if (ehdr_checked) {
- return ehdr;
- };
- ehdr_checked = true;
-
- for (let i = 0; linux::auxv[i].a_type != 0; i += 1) {
- if (linux::auxv[i].a_type != elf::at::SYSINFO_EHDR)
- continue;
- ehdr = linux::auxv[i].a_val: uintptr: *elf::header64;
- return ehdr;
- };
- return null;
-};
-
-type vdso_ctx = struct {
- segbase: uintptr,
- stringtab: *char,
- symtab: *[*]elf::sym64,
- hashhdr: *elf::hashhdr,
- versym: nullable *[*]u16,
- verdef: nullable *elf::verdef64,
-};
-
-let ctx: nullable *vdso_ctx = null;
-
-fn get_vdso_ctx() nullable *vdso_ctx = {
- static let vdso_checked = false;
- if (vdso_checked) {
- return ctx;
- };
- vdso_checked = true;
-
- const eh = match (sys_infoehdr()) {
- case null =>
- return null;
- case let x: *elf::header64 =>
- yield x;
- };
-
- const ehui = eh: uintptr;
- let phui = ehui + eh.e_phoff: uintptr;
- let dynvec: nullable *[*]elf::dyn64 = null;
- let baseseg: nullable *void = null;
-
- for (let i: u16 = 0; i < eh.e_phnum; i += 1) {
- const ph = phui: *elf::phdr64;
- switch (ph.p_type) {
- case elf::pt::LOAD =>
- baseseg = ehui +
- ph.p_offset: uintptr -
- ph.p_vaddr: uintptr;
- case elf::pt::DYNAMIC =>
- dynvec = ehui + ph.p_offset: uintptr;
- case => void;
- };
- phui += eh.e_phentsize: uintptr;
- };
-
- if (dynvec == null || baseseg == null) {
- return null;
- };
- const dynv = dynvec: *[*]elf::dyn64;
-
- let segbase = baseseg: uintptr;
- let stringtab: nullable *char = null;
- let symtab: nullable *[*]elf::sym64 = null;
- let hashhdr: nullable *elf::hashhdr = null;
- let versym: nullable *[*]u16 = null;
- let verdef: nullable *elf::verdef64 = null;
-
- for (let i = 0; dynv[i].d_tag != elf::dt::NULL; i += 1) {
- const tabptr = (segbase + dynv[i].d_val: uintptr): *void;
- switch (dynv[i].d_tag) {
- case elf::dt::STRTAB =>
- stringtab = tabptr: *char;
- case elf::dt::SYMTAB =>
- symtab = tabptr: *[*]elf::sym64;
- case elf::dt::HASH =>
- hashhdr = tabptr: *elf::hashhdr;
- case elf::dt::VERSYM =>
- versym = tabptr: *[*]u16;
- case elf::dt::VERDEF =>
- verdef = tabptr: *elf::verdef64;
- case =>
- continue;
- };
- };
-
- if (stringtab == null || symtab == null || hashhdr == null) {
- return null;
- };
-
- if (verdef == null) {
- versym = null;
- };
-
- // TODO: use a static variable here somehow(?)
- const vctx = alloc(vdso_ctx {
- segbase = segbase,
- stringtab = stringtab: *char,
- symtab = symtab: *[*]elf::sym64,
- hashhdr = hashhdr: *elf::hashhdr,
- verdef = verdef,
- versym = versym,
- });
- ctx = vctx;
-
- return ctx;
-};
-
-fn vdso_checkver(ctx: *vdso_ctx, version: str, num: u32) bool = {
- let prev = null: *elf::verdef64;
- let cur = match (ctx.verdef) {
- case null =>
- return true;
- case let vd: *elf::verdef64 =>
- yield vd;
- };
- const versym = match (ctx.versym) {
- case null =>
- return true;
- case let vs: *[*]u16 =>
- yield vs[num] & 0x7ff;
- };
- for (cur != prev) {
- if (cur.vd_flags & elf::ver_flg::BASE: u16 == 0 &&
- cur.vd_ndx & 0x7fff == versym) {
- const aux = (cur: uintptr +
- cur.vd_aux: uintptr): *elf::verdaux64;
- const name = ctx.stringtab: uintptr +
- aux.vda_name: uintptr;
- return version == strings::fromc(name: *char);
- };
- prev = cur;
- cur += cur.vd_next: uintptr;
- };
- return false;
-};
-
-export fn getsym(symname: str, symver: str) nullable *void = {
- const ctx = match (get_vdso_ctx()) {
- case null =>
- return null;
- case let x: *vdso_ctx =>
- yield x;
- };
-
- const sym_types = (1 << elf::stt::NOTYPE |
- 1 << elf::stt::OBJECT |
- 1 << elf::stt::FUNC |
- 1 << elf::stt::COMMON): size;
-
- const sym_binds = (1 << elf::stb::GLOBAL |
- 1 << elf::stb::WEAK): size;
-
- for (let i = 0u32; i < ctx.hashhdr.nchain; i += 1) {
- const sym = ctx.symtab[i];
- const symtype = 1 << (sym.st_info & 0xf): size;
- const symbind = 1 << (sym.st_info >> 4): size;
- if (symtype & sym_types == 0 || symbind & sym_binds == 0 ||
- sym.st_shndx == 0) {
- continue;
- };
- const name = ctx.stringtab: uintptr + sym.st_name: uintptr;
- const s: str = strings::fromc(name: *char);
- if (s != symname)
- continue;
- if (!vdso_checkver(ctx, symver, i))
- continue;
- return (ctx.segbase + sym.st_value: uintptr): *void;
- };
- return null;
-};