From patchwork Fri May 15 07:19:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1290913 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=deneb.enyo.de Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (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) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49Nfth5B99z9sT8 for ; Fri, 15 May 2020 17:19:56 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6214E3973056; Fri, 15 May 2020 07:19:54 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from albireo.enyo.de (albireo.enyo.de [37.24.231.21]) by sourceware.org (Postfix) with ESMTPS id 1FFD9386F41D for ; Fri, 15 May 2020 07:19:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1FFD9386F41D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=deneb.enyo.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=fw@deneb.enyo.de Received: from [172.17.203.2] (helo=deneb.enyo.de) by albireo.enyo.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) id 1jZUdA-0002vO-7D for libc-alpha@sourceware.org; Fri, 15 May 2020 07:19:40 +0000 Received: from fw by deneb.enyo.de with local (Exim 4.92) (envelope-from ) id 1jZUdA-0005K0-2O for libc-alpha@sourceware.org; Fri, 15 May 2020 09:19:40 +0200 From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH] Add scripts/move-symbol-to-libc.py Date: Fri, 15 May 2020 09:19:40 +0200 Message-ID: <871rnld5rn.fsf@mid.deneb.enyo.de> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, KAM_STOCKGEN, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" This helper script can be used to move symbols to libc.abilist across all architectures, while preserving their symbol version. --- scripts/move-symbol-to-libc.py | 157 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) diff --git a/scripts/move-symbol-to-libc.py b/scripts/move-symbol-to-libc.py new file mode 100644 index 0000000000..2ed687caa5 --- /dev/null +++ b/scripts/move-symbol-to-libc.py @@ -0,0 +1,157 @@ +#!/usr/bin/python3 +# Move symbols from other shared objects into libc.so. +# Copyright (C) 2020 Free Software Foundation, Inc. +# This file is part of the GNU C Library. +# +# The GNU C Library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# The GNU C Library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with the GNU C Library; if not, see +# . + +"""Move symbols from other shared objects into libc.so. + +This script moves ABI symbols from non-libc abilists in to +libc.abilist. Symbol versions are preserved. The script must be +called from the top of the glibc source tree. + +""" + +import argparse +import os.path +import sys + +def replace_file(path, new_contents): + """Atomically replace PATH with lines from NEW_CONTENTS. + + NEW_CONTENTS must be a sequence of strings. + + """ + temppath = path + 'T' + with open(temppath, 'w') as out: + for line in new_contents: + out.write(line) + os.rename(temppath, path) + +def add_to_libc_path(path, symbol_lines): + """Add SYMBOL_LINES (a list of strings) to the abilist file PATH.""" + with open(path) as inp: + original_lines = list(inp) + new_lines = set(original_lines) + new_lines.update(symbol_lines) + new_lines = list(new_lines) + new_lines.sort() + if new_lines != original_lines: + sys.stdout.write('updating libc abilist {!r}\n'.format(path)) + replace_file(path, new_lines) + +# The name of the libc.so abilist file. +libc_abilist = 'libc.abilist' + +def add_to_libc_fallback(directory, subdirs, symbol_lines): + """Add SYMBOL_LINES to the libc.abilist files in SUBDIRS in DIRECTORY. + + All subdirectories must exist. If they do, return True. If not, + skip processing and return False. + + """ + abilists = [os.path.join(directory, subdir, libc_abilist) + for subdir in subdirs] + for abilist in abilists: + if not os.path.exists(abilist): + return False + for abilist in abilists: + add_to_libc_path(abilist, symbol_lines) + return True + +def add_to_libc(directory, symbol_lines): + + """Add SYMBOL_LINES (a list of strings) to libc.abilist in DIRECTORY. + + Try specific subdirectories as well if libc.abilist is not found + in DIRECTORY. + + """ + libc_path = os.path.join(directory, libc_abilist) + if os.path.exists(libc_path): + add_to_libc_path(libc_path, symbol_lines) + return + + # Special case for powerpc32 and mips32 variants. + if add_to_libc_fallback(directory, ('fpu', 'nofpu'), symbol_lines): + return + + # Special case for mips64. + if add_to_libc_fallback(directory, ('n32', 'n64'), symbol_lines): + return + + raise IOError('No libc.abilist found for: {!r}'.format(directory)) + +def move_symbols_1(path, symbols, moved_symbols): + """Move SYMBOLS from the abilist file PATH to MOVED_SYMBOLS. + + SYMBOLS must be a set of strings. MOVED_SYMBOLS is a list. + + """ + new_lines = [] + changed = False + with open(path) as inp: + for line in inp: + version, symbol, flags = line.split(' ', 2) + if symbol in symbols: + moved_symbols.append(line) + changed = True + else: + new_lines.append(line) + if changed: + sys.stdout.write('updating source abilist {!r}\n'.format(path)) + replace_file(path, new_lines) + +def move_symbols(directory, files, symbols): + """Move SYMBOLS from FILES (a list of abilist file names) in DIRECTORY. + + SYMBOLS must be a set of strings. + + """ + moved_symbols = [] + for filename in files: + move_symbols_1(os.path.join(directory, filename), symbols, + moved_symbols) + if moved_symbols: + add_to_libc(directory, moved_symbols) + +def get_parser(): + """Return an argument parser for this module.""" + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--only-linux', action='store_true', + help='Restrict the operation to Linux abilists') + parser.add_argument('symbols', help='name of the symbol to move', + nargs='+') + return parser + +def main(argv): + """The main entry point.""" + parser = get_parser() + opts = parser.parse_args(argv) + if opts.only_linux: + sysdeps = 'sysdeps/unix/sysv/linux' + else: + sysdeps = 'sysdeps' + + symbols = frozenset(opts.symbols) + + for directory, dirs, files in os.walk(sysdeps): + move_symbols(directory, [name for name in files + if name != 'libc.abilist' + and name.endswith('.abilist')], symbols) + +if __name__ == '__main__': + main(sys.argv[1:])