From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by sourceware.org (Postfix) with ESMTPS id 564F43858D20 for ; Wed, 15 Nov 2023 01:26:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 564F43858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 564F43858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700011593; cv=none; b=E1Pgwd8XtG/IAXruxmRMWIN09N/JpfwkB8Yb0ElUe9MfgoSmvYnpBflbZnAW2W9fRHaazlA++rHjBN/FuvVh/Kxiaqy3KG2TRCzpKBuLLosFaCaA0Y2YSg3Bno3PXvaLAi3Jee9fPYlriqarVFdx2MVONXc7HJBbRYfiPYlJ3o4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700011593; c=relaxed/simple; bh=K2ITaQKYJecTH56EhrwYOHEPXygS2qAEGiaeDdtIZfg=; h=DKIM-Signature:Date:Message-ID:From:To:Subject; b=F8o4gh4wm8PDhL63laP1PcdWKBxlgrMqOknOahInWL+P/QPBnPXdWAQAelR9mHMsDfM7sdJwDjIWOOEVOePL03ek/omD0qZStsYgLdR6Y+wO7pYCMUvxBQGeJDC/imwX7TPD5Kpuc9EPJX3IYB3tDE63hH6yxQmcNcW8RwMujuk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-6c398717726so5441998b3a.2 for ; Tue, 14 Nov 2023 17:26:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700011590; x=1700616390; darn=sourceware.org; h=subject:in-reply-to:to:from:message-id:date:from:to:cc:subject:date :message-id:reply-to; bh=yIFzCg4lyiCo5w/q3jZj2FWD7kdZIQe3uJd1othO0Dk=; b=eV8AdO438BALRJ/ntyZzT5+uWRn2jb8lAP+X0ByOcjtDB2DjLIo5jLOJlwMmtzmYqt SBWcJ2yu6+5gIoOsoMz2XCGg4HL73BMP6n4A+5TDM6qAwVu2cFcjhY/FZAj80J9zceYN +KCd7ShTuq6ukDXkwzmk20mb2L4cT1svFt/Ayut+5+YU+OHIuPRpCxQiFHGpI3jt9aFP 5lusH0h/mAfwmaCM2VeolgNVOGkm1Nk85pN+9Xq6IgJgcOONEh6C2rxBn+MMJf4w1ssI W9G66cuwtGmWWwYPW862XyOyCuQxqFdhM1NN9b+Q7JwxeBXT/c8yW7lpjzWOiMCC+lKR 2u5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700011590; x=1700616390; h=subject:in-reply-to:to:from:message-id:date:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=yIFzCg4lyiCo5w/q3jZj2FWD7kdZIQe3uJd1othO0Dk=; b=XTg7nhe9brfAgzCw2ue0jF9bKjpWMzxJfMh61IYmkiMXih4h+oUbcf6ikz9nhR07Xr gIhoev4WzAYZLvJk0li5SzhfTDhYcR5NRPoeVqS5coDj+DAlb52EimalVpoiIFlp4Upj KlgQbRKDiNibwUKUKj5GMix861O7arCDsNx+/vwO5MwmeDFV1OeaVx1XfJAEzJegYilg gG26C7HpdhNFmf/74A6MQQh7WPRaj8BUwDg5gQmJR76fgZHVu98cCMDab7Aa4l7ioS84 xgUNybmY+c/1M6R71BniEnHej22rE2MfNGwBjzJCTJ+hNh7U6MXwZHbGUpD7LE1ASCGc B47A== X-Gm-Message-State: AOJu0Yz6mrl1BH3aA24agMJSFRMNcBzCI1JZRfrHMZcMw9RWADELdUtC 0ab1HUAelLM+jGQa93RucMrLZ2eiTKU= X-Google-Smtp-Source: AGHT+IFlFQnDrbo56V3zgwq75hQ+sYefKZFWVnVFpg/dGMIKlZmrkCCnOcbhsnAesp3wqLsfuYUoZA== X-Received: by 2002:a05:6a20:8e25:b0:186:8024:90d5 with SMTP id y37-20020a056a208e2500b00186802490d5mr7585038pzj.19.1700011589817; Tue, 14 Nov 2023 17:26:29 -0800 (PST) Received: from localhost ([2804:7f0:3990:c9ca:141:6e80:cb8e:e051]) by smtp.gmail.com with ESMTPSA id 4-20020aa79204000000b006b2677d3685sm1783552pfo.207.2023.11.14.17.26.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Nov 2023 17:26:29 -0800 (PST) Date: Tue, 14 Nov 2023 22:26:28 -0300 Message-ID: From: Matheus Afonso Martins Moreira To: binutils@sourceware.org In-Reply-To: Subject: Re: Appending program headers to existing ELFs? X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Hello Nick, > Why can't you add the data at link time ? I'd like to add a section to the executable that users can freely modify after the binary has been built and distributed without needing to recompile or relink anything. In concrete terms: I'm building a small programming language interpreter and would like to add support for embedding programs into the executable. My intention is to treat the embedded programs section as an additional load path for libraries and modules as well as the main function. > could you create a new symbol for the section > and reference the data that way That is indeed possible but as you noted it would require access to the symbol table at runtime and that's not guaranteed. That information may have been stripped away and the kernel does not load it to begin with. I'm also not using libc so I don't have access to dlopen and dlsym. > At the moment this can only be done by the linker I see. I suspected as much but hoped there would be a way. Thanks for clarifying that. > With a linker script you can layout a PHDRS structure Yes. It's one of the solutions I tried. I copied ld's internal linker script and customized it with a PHDRS command: PHDRS { headers PT_PHDR FILEHDR PHDRS; text PT_LOAD; rodata PT_LOAD; data PT_LOAD; bss PT_LOAD; extra PT_LOAD; /* meant to be empty */ /* size/offset will be set later */ } I was not successful. I keep getting the following error: error: could not allocate headers I was unable to determine the cause and fix it. For ease of maintenance, I don't intend to mess with the sections and segments generated by the GNU tools. I would have liked to merely append a custom header to the ones generated by ld. Unfortunately, merely including a PHDRS command disables ld's program header generation logic which sensibly maps the sections to the headers. A linker script directive that applies defaults while allowing further customization would be a great new feature. In the meantime, I'm trying to work around the problem by placing a statically allocated packed structure into an isolated section so that its entire contents can be overwritten by objcopy: objcopy --update-section .structure=./file lang lang+file This reduces the problem to a binary file format problem and seems like the way forward for me at the moment. Would have been nice to be able to reuse the ELF machinery though... Thanks for the guidance, Matheus