From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by sourceware.org (Postfix) with ESMTPS id D326C385781D for ; Sat, 9 Jul 2022 12:24:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D326C385781D Received: by mail-pl1-x62e.google.com with SMTP id 5so892940plk.9 for ; Sat, 09 Jul 2022 05:24:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition; bh=slTEH00gTyccFJ5UQ+7FH+d6PAqnMMDjq5meUwxJ7k0=; b=A5DbQyW1YMqkJN+As4uYT1O2aOkUUwzRNdbhU8LV0ftsqzpfYp/lf8zWzW2CunGOM4 QRXoDgQjxOOnwIssfSHCb4r6XUkgXoR5cKROVTqxDO2VGWLkuwYjrbpoY4q8vKK0mXSS qyrN+G5fc75oBO3ynXVVO4xgZUPl43hqKlkBFaJr+Bq29jUS1MjnotLtRn0/F7wzuPH+ WVl/q2qjQi7kDRrNxGEDj7J3sBhAMnXZig+d4fTwvLmyJO5v+D7PGBWXLqkrDgPl6Rx9 U0WVljahVkWM0JwRcjKdlN8rCMKIQp2Uh8G5Yy2aJKvR/9CjdSIb3Z7wHNxEnMw5av9W 06qw== X-Gm-Message-State: AJIora+giRfxN2s7YBQKsx3rS7TmiE4ZMPX3D46HFWyrSkoR987hYDMc 5B/yeXjv/b4aCk2jteFsgXxxlJnV3Nc= X-Google-Smtp-Source: AGRyM1vqeoThNe8nxpqID3TJBzdVSm6BJdLU9A2vVaQsLUADM/6HIQ1QidKlgHG2/Ys0vSdKiNhvHQ== X-Received: by 2002:a17:90b:4c92:b0:1f0:1657:fd9c with SMTP id my18-20020a17090b4c9200b001f01657fd9cmr1887380pjb.230.1657369486445; Sat, 09 Jul 2022 05:24:46 -0700 (PDT) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id l9-20020a635b49000000b0040d376ea100sm1016221pgm.73.2022.07.09.05.24.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Jul 2022 05:24:46 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 63A181143046; Sat, 9 Jul 2022 21:54:43 +0930 (ACST) Date: Sat, 9 Jul 2022 21:54:43 +0930 From: Alan Modra To: binutils@sourceware.org Subject: gas: utility notes memory alloc functions Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3037.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 09 Jul 2022 12:24:49 -0000 Makes it a little easier to use the notes obstack for persistent storage. * as.h (gas_mul_overflow): Define. * symbols.h (notes_alloc, notes_calloc, notes_memdup), (notes_strdup, notes_concat, notes_free): Declare. * symbols.c (notes_alloc, notes_calloc, notes_memdup), (notes_strdup, notes_concat, notes_free): New functions. (save_symbol_name): Use notes_strdup. (symbol_create, local_symbol_make, local_symbol_convert), (symbol_clone, decode_local_label_name): Use notes_alloc. diff --git a/gas/as.h b/gas/as.h index 4fac7d21a32..0ee3873bd1a 100644 --- a/gas/as.h +++ b/gas/as.h @@ -122,6 +122,14 @@ void *mempcpy(void *, const void *, size_t); #define xfree free +#if GCC_VERSION >= 7000 +#define gas_mul_overflow(a, b, res) __builtin_mul_overflow (a, b, res) +#else +/* Assumes unsigned values. Careful! Args evaluated multiple times. */ +#define gas_mul_overflow(a, b, res) \ + ((*res) = (a), (*res) *= (b), (b) != 0 && (*res) / (b) != (a)) +#endif + #include "asintl.h" #define BAD_CASE(val) \ diff --git a/gas/symbols.c b/gas/symbols.c index e3fddee8c79..00ae49b91ed 100644 --- a/gas/symbols.c +++ b/gas/symbols.c @@ -242,6 +242,75 @@ struct xsymbol dot_symbol_x; #endif struct obstack notes; + +/* Utility functions to allocate and duplicate memory on the notes + obstack, each like the corresponding function without "notes_" + prefix. All of these exit on an allocation failure. */ + +void * +notes_alloc (size_t size) +{ + return obstack_alloc (¬es, size); +} + +void * +notes_calloc (size_t n, size_t size) +{ + size_t amt; + void *ret; + if (gas_mul_overflow (n, size, &amt)) + { + obstack_alloc_failed_handler (); + abort (); + } + ret = notes_alloc (amt); + memset (ret, 0, amt); + return ret; +} + +void * +notes_memdup (const void *src, size_t copy_size, size_t alloc_size) +{ + void *ret = obstack_alloc (¬es, alloc_size); + memcpy (ret, src, copy_size); + if (alloc_size > copy_size) + memset ((char *) ret + copy_size, 0, alloc_size - copy_size); + return ret; +} + +char * +notes_strdup (const char *str) +{ + size_t len = strlen (str) + 1; + return notes_memdup (str, len, len); +} + +char * +notes_concat (const char *first, ...) +{ + va_list args; + const char *str; + + va_start (args, first); + for (str = first; str; str = va_arg (args, const char *)) + { + size_t size = strlen (str); + obstack_grow (¬es, str, size); + } + va_end (args); + obstack_1grow (¬es, 0); + return obstack_finish (¬es); +} + +/* Use with caution! Frees PTR and all more recently allocated memory + on the notes obstack. */ + +void +notes_free (void *ptr) +{ + obstack_free (¬es, ptr); +} + #ifdef TE_PE /* The name of an external symbol which is used to make weak PE symbol names unique. */ @@ -274,13 +343,10 @@ symbol_new (const char *name, segT segment, fragS *frag, valueT valu) static const char * save_symbol_name (const char *name) { - size_t name_length; char *ret; gas_assert (name != NULL); - name_length = strlen (name) + 1; /* +1 for \0. */ - obstack_grow (¬es, name, name_length); - ret = (char *) obstack_finish (¬es); + ret = notes_strdup (name); #ifdef tc_canonicalize_symbol_name ret = tc_canonicalize_symbol_name (ret); @@ -343,7 +409,7 @@ symbol_create (const char *name, segT segment, fragS *frag, valueT valu) preserved_copy_of_name = save_symbol_name (name); size = sizeof (symbolS) + sizeof (struct xsymbol); - symbolP = (symbolS *) obstack_alloc (¬es, size); + symbolP = notes_alloc (size); /* symbol must be born in some fixed state. This seems as good as any. */ memset (symbolP, 0, size); @@ -377,7 +443,7 @@ local_symbol_make (const char *name, segT section, fragS *frag, valueT val) name_copy = save_symbol_name (name); - ret = (struct local_symbol *) obstack_alloc (¬es, sizeof *ret); + ret = notes_alloc (sizeof *ret); ret->flags = flags; ret->hash = 0; ret->name = name_copy; @@ -403,7 +469,7 @@ local_symbol_convert (void *sym) ++local_symbol_conversion_count; - xtra = (struct xsymbol *) obstack_alloc (¬es, sizeof (*xtra)); + xtra = notes_alloc (sizeof (*xtra)); memset (xtra, 0, sizeof (*xtra)); val = ent->lsy.value; ent->sy.x = xtra; @@ -717,8 +783,7 @@ symbol_clone (symbolS *orgsymP, int replace) orgsymP = local_symbol_convert (orgsymP); bsymorg = orgsymP->bsym; - newsymP = (symbolS *) obstack_alloc (¬es, (sizeof (symbolS) - + sizeof (struct xsymbol))); + newsymP = notes_alloc (sizeof (symbolS) + sizeof (struct xsymbol)); *newsymP = *orgsymP; newsymP->x = (struct xsymbol *) (newsymP + 1); *newsymP->x = *orgsymP->x; @@ -2104,7 +2169,7 @@ decode_local_label_name (char *s) instance_number = (10 * instance_number) + *p - '0'; message_format = _("\"%d\" (instance number %d of a %s label)"); - symbol_decode = (char *) obstack_alloc (¬es, strlen (message_format) + 30); + symbol_decode = notes_alloc (strlen (message_format) + 30); sprintf (symbol_decode, message_format, label_number, instance_number, type); return symbol_decode; diff --git a/gas/symbols.h b/gas/symbols.h index 19eb658ca68..240e08f8df6 100644 --- a/gas/symbols.h +++ b/gas/symbols.h @@ -35,6 +35,13 @@ extern int symbol_table_frozen; default. */ extern int symbols_case_sensitive; +extern void *notes_alloc (size_t); +extern void *notes_calloc (size_t, size_t); +extern void *notes_memdup (const void *, size_t, size_t); +extern char *notes_strdup (const char *); +extern char *notes_concat (const char *, ...); +extern void notes_free (void *); + char * symbol_relc_make_expr (expressionS *); char * symbol_relc_make_sym (symbolS *); char * symbol_relc_make_value (offsetT); -- Alan Modra Australia Development Lab, IBM