From nobody Wed Apr 26 11:12:40 2023 X-Original-To: freebsd-current@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Q5x6n6DNPz47qbv for ; Wed, 26 Apr 2023 11:12:49 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4Q5x6m50M7z3nJy; Wed, 26 Apr 2023 11:12:48 +0000 (UTC) (envelope-from kostikbel@gmail.com) Authentication-Results: mx1.freebsd.org; none Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.17.1/8.17.1) with ESMTPS id 33QBCeur017043 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Wed, 26 Apr 2023 14:12:43 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua 33QBCeur017043 Received: (from kostik@localhost) by tom.home (8.17.1/8.17.1/Submit) id 33QBCe9Q017042; Wed, 26 Apr 2023 14:12:40 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Wed, 26 Apr 2023 14:12:40 +0300 From: Konstantin Belousov To: Hans Petter Selasky Cc: Zhenlei Huang , FreeBSD CURRENT , Gleb Smirnoff Subject: Re: Link modules to DYN type Message-ID: References: <97390FE1-1DF5-43A1-A3F4-2B945D681437@FreeBSD.org> <2bb66cac-c7f1-e45b-693a-8afbda05cfa6@freebsd.org> List-Id: Discussions about the use of FreeBSD-current List-Archive: https://lists.freebsd.org/archives/freebsd-current List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-current@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <2bb66cac-c7f1-e45b-693a-8afbda05cfa6@freebsd.org> X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=4.0.0 X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-14) on tom.home X-Rspamd-Queue-Id: 4Q5x6m50M7z3nJy X-Spamd-Bar: ---- X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:6939, ipnet:2001:470::/32, country:US] X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-ThisMailContainsUnwantedMimeParts: N On Wed, Apr 26, 2023 at 12:55:02PM +0200, Hans Petter Selasky wrote: > On 4/26/23 12:36, Zhenlei Huang wrote: > > Hi, > > > > I'm recently working on https://reviews.freebsd.org/D39638 (sysctl(9): Enable vnet sysctl variables be loader tunable), > > the changes to `sys/kern/link_elf_obj.c` are runtime tested, but not those to `sys/kern/link_elf.c` . > > > > After some hacking I realized that `link_elf.c` is for EXEC (Executable file) or DYN (Shared object file), and `link_elf_obj.c` is > > for REL (Relocatable file). > > > > ``` > > /* link_elf.c */ > > static int > > link_elf_load_file(linker_class_t cls, const char* filename, > > linker_file_t* result) > > { > > ... > > if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) { > > error = ENOSYS; > > goto out; > > } > > ... > > } > > > > > > /* link_elf_obj.c */ > > static int > > link_elf_load_file(linker_class_t cls, const char *filename, > > linker_file_t *result) > > { > > ... > > if (hdr->e_type != ET_REL) { > > error = ENOSYS; > > goto out; > > } > > ... > > } > > ``` > > > > Run the following snip: > > ``` > > # find /boot/kernel -type f -name "*.ko" -exec readelf -h {} \; | grep Type > > ``` > > shows that all the kernel modules' types are `REL (Relocatable file)`. > > > > I guess if some module such as if_bridge is linked to DYN type, then I can do runtime for the changes to `sys/kern/link_elf.c`. > > > > I'm not familiar with elf and linkers, is that ( compile module and link it to DYN type ) possible ? Module file type (shared object vs. object file) depends on architecture. For amd64 modules are objects, while kernel is shared library. For arm64 (and all other arches, I believe) modules and kernels are shared libraries. I think you can link amd64 module as shared object, but this require enough hacking of the build infrastructure. At least I am not aware of a simple knob to switch the produced type. > > > > Hi, > > I don't have an answer for you either, but I have seen in the past, loading > kernel modules behaves a bit like libraries, in the following regard: > > If two kernel modules define the same global symbol, then no warning is > given and the first loaded symbol definition (I think) is used to resolve > that symbol for all kernel modules, regardless of the prototype. Probably we > should not allow this. That's why building LINT is a good thing, to avoid > this issue. No, in-kernel linker does not behave this way. Modules need to contain explicit reference to all modules they depend upon, using the MODULE_DEPEND() macro. Only symbols from the dependencies are resolved. All modules get an implicit reference to kernel. > > Even if we don't have C++ support in the FreeBSD kernel, defining symbol > names the way C++ does for C could be nice for the kernel too, also with > regards to debugging systems. > > Many times when I don't know what is going on, I do like this: > > #include > > .... > > if (not too fast or my sysctl debug) { > printf("My tracer\n"); > kdb_backtrace(); > } > > Dtrace can also do this, but not during boot. Just track who is calling > those functions, and you'll probably find the answer to your question! > > --HPS > > > > > Best regards, > > Zhenlei > > >