LCOV - code coverage report
Current view: top level - gcc - btfout.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.1 % 713 678
Test Date: 2025-10-18 14:39:06 Functions: 87.5 % 56 49
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Output BTF format from GCC.
       2                 :             :    Copyright (C) 2021-2025 Free Software Foundation, Inc.
       3                 :             : 
       4                 :             : This file is part of GCC.
       5                 :             : 
       6                 :             : GCC is free software; you can redistribute it and/or modify it under
       7                 :             : the terms of the GNU General Public License as published by the Free
       8                 :             : Software Foundation; either version 3, or (at your option) any later
       9                 :             : version.
      10                 :             : 
      11                 :             : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      12                 :             : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13                 :             : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14                 :             : for more details.
      15                 :             : 
      16                 :             : You should have received a copy of the GNU General Public License
      17                 :             : along with GCC; see the file COPYING3.  If not see
      18                 :             : <http://www.gnu.org/licenses/>.  */
      19                 :             : 
      20                 :             : /* This file contains routines to output the BPF Type Format (BTF). The BTF
      21                 :             :    debug format is very similar to CTF; as a result, the structure of this file
      22                 :             :    closely resembles that of ctfout.cc, and the same CTF container objects are
      23                 :             :    used.  */
      24                 :             : 
      25                 :             : #include "config.h"
      26                 :             : #include "system.h"
      27                 :             : #include "coretypes.h"
      28                 :             : #include "target.h"
      29                 :             : #include "memmodel.h"
      30                 :             : #include "tm_p.h"
      31                 :             : #include "output.h"
      32                 :             : #include "dwarf2asm.h"
      33                 :             : #include "debug.h"
      34                 :             : #include "ctfc.h"
      35                 :             : #include "diagnostic-core.h"
      36                 :             : #include "cgraph.h"
      37                 :             : #include "varasm.h"
      38                 :             : #include "stringpool.h"  /* For lookup_attribute.  */
      39                 :             : #include "attribs.h" /* For lookup_attribute.  */
      40                 :             : #include "dwarf2out.h" /* For lookup_decl_die.  */
      41                 :             : 
      42                 :             : static int btf_label_num;
      43                 :             : 
      44                 :             : static GTY (()) section * btf_info_section;
      45                 :             : 
      46                 :             : /* BTF debug info section.  */
      47                 :             : 
      48                 :             : #ifndef BTF_INFO_SECTION_NAME
      49                 :             : #define BTF_INFO_SECTION_NAME  ".BTF"
      50                 :             : #endif
      51                 :             : 
      52                 :             : #define BTF_INFO_SECTION_FLAGS (SECTION_DEBUG)
      53                 :             : 
      54                 :             : /* Maximum size (in bytes) for an artifically generated BTF label.  */
      55                 :             : 
      56                 :             : #define MAX_BTF_LABEL_BYTES 40
      57                 :             : 
      58                 :             : static char btf_info_section_label[MAX_BTF_LABEL_BYTES];
      59                 :             : 
      60                 :             : #ifndef BTF_INFO_SECTION_LABEL
      61                 :             : #define BTF_INFO_SECTION_LABEL  "Lbtf"
      62                 :             : #endif
      63                 :             : 
      64                 :             : #define BTF_INVALID_TYPEID 0xFFFFFFFF
      65                 :             : 
      66                 :             : /* Internal representation of an entry in a BTF_KIND_DATASEC record.  */
      67                 :             : struct btf_datasec_entry
      68                 :             : {
      69                 :             :   union {
      70                 :             :     ctf_dvdef_ref dvd; /* Reference to the underlying variable represented.  */
      71                 :             :     ctf_dtdef_ref dtd; /* Reference to the underlying type represented.  */
      72                 :             :   };
      73                 :             :   bool is_var;         /* True iff this entry represents a variable.  */
      74                 :             :   uint32_t size;       /* Size of variable or function, in bytes.
      75                 :             :                           For functions, always zero at compile time.  */
      76                 :             : };
      77                 :             : 
      78                 :             : /* Internal representation of a BTF_KIND_DATASEC record.  */
      79                 :             : typedef struct btf_datasec
      80                 :             : {
      81                 :             :   ctf_id_t id;                           /* BTF type ID of this record.  */
      82                 :             :   const char *name;                      /* Section name, e.g. ".bss".  */
      83                 :             :   uint32_t name_offset;                  /* Offset to name in string table.  */
      84                 :             :   vec<struct btf_datasec_entry> entries; /* Entries in this section.  */
      85                 :             : } btf_datasec_t;
      86                 :             : 
      87                 :             : /* One BTF_KIND_DATASEC record is created for each output data section which
      88                 :             :    will hold at least one variable.  */
      89                 :             : static vec<btf_datasec_t> datasecs;
      90                 :             : 
      91                 :             : /* Functions in BTF have two separate type records - one for the prototype
      92                 :             :    (BTF_KIND_FUNC_PROTO), as well as a BTF_KIND_FUNC. CTF_K_FUNCTION types
      93                 :             :    map closely to BTF_KIND_FUNC_PROTO, but the BTF_KIND_FUNC records must be
      94                 :             :    created. This vector holds them.  */
      95                 :             : static GTY (()) vec<ctf_dtdef_ref, va_gc> *funcs;
      96                 :             : 
      97                 :             : /* Maps BTF_KIND_FUNC_PROTO to the BTF_KIND_FUNC record for it.  Used when
      98                 :             :    creating DATASEC entries.  */
      99                 :             : static GTY (()) hash_map<ctf_dtdef_ref, ctf_dtdef_ref> *func_map;
     100                 :             : 
     101                 :             : /* Highest BTF ID assigned to any regular type translated from CTF.
     102                 :             :    Does not include BTF_KIND_{VAR,FUNC,DATASEC} types.  */
     103                 :             : static ctf_id_t max_translated_id = 0;
     104                 :             : 
     105                 :             : /* Name strings for BTF kinds.
     106                 :             :    Note: the indices here must match the type defines in btf.h.  */
     107                 :             : static const char *const btf_kind_names[] =
     108                 :             :   {
     109                 :             :     "UNKN", "INT", "PTR", "ARRAY", "STRUCT", "UNION", "ENUM", "FWD",
     110                 :             :     "TYPEDEF", "VOLATILE", "CONST", "RESTRICT", "FUNC", "FUNC_PROTO",
     111                 :             :     "VAR", "DATASEC", "FLOAT", "DECL_TAG", "TYPE_TAG", "ENUM64"
     112                 :             :   };
     113                 :             : 
     114                 :             : /* Return a name string for the given BTF_KIND.  */
     115                 :             : 
     116                 :             : static const char *
     117                 :         947 : btf_kind_name (uint32_t btf_kind)
     118                 :             : {
     119                 :         947 :   return btf_kind_names[btf_kind];
     120                 :             : }
     121                 :             : 
     122                 :             : /* Map a CTF type kind to the corresponding BTF type kind.  */
     123                 :             : 
     124                 :             : static uint32_t
     125                 :        3848 : get_btf_kind (uint32_t ctf_kind)
     126                 :             : {
     127                 :             :   /* N.B. the values encoding kinds are not in general the same for the
     128                 :             :      same kind between CTF and BTF. e.g. CTF_K_CONST != BTF_KIND_CONST.  */
     129                 :           0 :   switch (ctf_kind)
     130                 :             :     {
     131                 :             :     case CTF_K_INTEGER:  return BTF_KIND_INT;
     132                 :             :     case CTF_K_FLOAT:    return BTF_KIND_FLOAT;
     133                 :             :     case CTF_K_POINTER:  return BTF_KIND_PTR;
     134                 :             :     case CTF_K_ARRAY:    return BTF_KIND_ARRAY;
     135                 :             :     case CTF_K_FUNCTION: return BTF_KIND_FUNC_PROTO;
     136                 :             :     case CTF_K_STRUCT:   return BTF_KIND_STRUCT;
     137                 :             :     case CTF_K_UNION:    return BTF_KIND_UNION;
     138                 :             :     case CTF_K_ENUM:     return BTF_KIND_ENUM;
     139                 :             :     case CTF_K_FORWARD:  return BTF_KIND_FWD;
     140                 :             :     case CTF_K_TYPEDEF:  return BTF_KIND_TYPEDEF;
     141                 :             :     case CTF_K_VOLATILE: return BTF_KIND_VOLATILE;
     142                 :             :     case CTF_K_CONST:    return BTF_KIND_CONST;
     143                 :             :     case CTF_K_RESTRICT: return BTF_KIND_RESTRICT;
     144                 :             :     case CTF_K_DECL_TAG: return BTF_KIND_DECL_TAG;
     145                 :             :     case CTF_K_TYPE_TAG: return BTF_KIND_TYPE_TAG;
     146                 :             :     default:;
     147                 :             :     }
     148                 :             :   return BTF_KIND_UNKN;
     149                 :             : }
     150                 :             : 
     151                 :             : /* Convenience wrapper around get_btf_kind for the common case.  */
     152                 :             : 
     153                 :             : uint32_t
     154                 :        3781 : btf_dtd_kind (ctf_dtdef_ref dtd)
     155                 :             : {
     156                 :        3781 :   if (!dtd)
     157                 :             :     return BTF_KIND_UNKN;
     158                 :        3781 :   return get_btf_kind (CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info));
     159                 :             : }
     160                 :             : 
     161                 :             : /* Some BTF types, like BTF_KIND_FUNC_PROTO, are anonymous.  The machinery
     162                 :             :    in btfout to emit BTF, may reset dtd_data->ctti_name, but does not update
     163                 :             :    the name in the ctf_dtdef_ref type object (deliberate choice).  This
     164                 :             :    interface helps abstract out that state of affairs, while giving access to
     165                 :             :    the name of the type as intended.  */
     166                 :             : 
     167                 :             : static const char *
     168                 :        1030 : get_btf_type_name (ctf_dtdef_ref dtd)
     169                 :             : {
     170                 :        1030 :   const char *anon = "";
     171                 :         656 :   return (dtd->dtd_data.ctti_name) ? dtd->dtd_name : anon;
     172                 :             : }
     173                 :             : 
     174                 :             : static bool
     175                 :        1445 : btf_emit_type_p (ctf_dtdef_ref dtd)
     176                 :             : {
     177                 :        1445 :   uint32_t kind = btf_dtd_kind (dtd);
     178                 :             : 
     179                 :        1445 :   if (kind == BTF_KIND_UNKN)
     180                 :             :     /* This type is not representable in BTF.  */
     181                 :             :     return false;
     182                 :             : 
     183                 :        1418 :   if (kind == BTF_KIND_INT && dtd->dtd_data.ctti_size == 0)
     184                 :             :     /* This is a (redundant) definition of void.  */
     185                 :          22 :     return false;
     186                 :             : 
     187                 :             :   return true;
     188                 :             : }
     189                 :             : 
     190                 :             : /* Return true if DTD is a forward-declared enum.  The BTF representation
     191                 :             :    of forward declared enums is not formally defined.  */
     192                 :             : 
     193                 :             : static bool
     194                 :         521 : btf_fwd_to_enum_p (ctf_dtdef_ref dtd)
     195                 :             : {
     196                 :         521 :   uint32_t kind = btf_dtd_kind (dtd);
     197                 :         521 :   return (kind == BTF_KIND_FWD && dtd->dtd_data.ctti_type == CTF_K_ENUM);
     198                 :             : }
     199                 :             : 
     200                 :             : /* Each BTF type can be followed additional, variable-length information
     201                 :             :    completing the description of the type. Calculate the number of bytes
     202                 :             :    of variable information required to encode a given type.  */
     203                 :             : 
     204                 :             : static uint64_t
     205                 :         426 : btf_calc_num_vbytes (ctf_dtdef_ref dtd)
     206                 :             : {
     207                 :         426 :   uint64_t vlen_bytes = 0;
     208                 :             : 
     209                 :         426 :   uint32_t kind = btf_dtd_kind (dtd);
     210                 :         426 :   uint32_t vlen = CTF_V2_INFO_VLEN (dtd->dtd_data.ctti_info);
     211                 :             : 
     212                 :         426 :   switch (kind)
     213                 :             :     {
     214                 :             :     case BTF_KIND_UNKN:
     215                 :             :     case BTF_KIND_PTR:
     216                 :             :     case BTF_KIND_FWD:
     217                 :             :     case BTF_KIND_TYPEDEF:
     218                 :             :     case BTF_KIND_VOLATILE:
     219                 :             :     case BTF_KIND_CONST:
     220                 :             :     case BTF_KIND_RESTRICT:
     221                 :             :     case BTF_KIND_FUNC:
     222                 :             :     case BTF_KIND_TYPE_TAG:
     223                 :             :     /* These kinds have no vlen data.  */
     224                 :             :       break;
     225                 :             : 
     226                 :         153 :     case BTF_KIND_INT:
     227                 :             :       /* Size 0 integers represent redundant definitions of void that will
     228                 :             :          not be emitted. Don't allocate space for them.  */
     229                 :         153 :       if (dtd->dtd_data.ctti_size == 0)
     230                 :             :         break;
     231                 :             : 
     232                 :         426 :       vlen_bytes += sizeof (uint32_t);
     233                 :             :       break;
     234                 :             : 
     235                 :             :     case BTF_KIND_ARRAY:
     236                 :         426 :       vlen_bytes += sizeof (struct btf_array);
     237                 :             :       break;
     238                 :             : 
     239                 :          35 :     case BTF_KIND_STRUCT:
     240                 :          35 :     case BTF_KIND_UNION:
     241                 :          35 :       vlen_bytes += vlen * sizeof (struct btf_member);
     242                 :          35 :       break;
     243                 :             : 
     244                 :           8 :     case BTF_KIND_ENUM:
     245                 :           8 :       vlen_bytes += (dtd->dtd_data.ctti_size > 4)
     246                 :           8 :                         ? vlen * sizeof (struct btf_enum64)
     247                 :           5 :                         : vlen * sizeof (struct btf_enum);
     248                 :             :       break;
     249                 :             : 
     250                 :          85 :     case BTF_KIND_FUNC_PROTO:
     251                 :          85 :       vlen_bytes += vlen * sizeof (struct btf_param);
     252                 :          85 :       break;
     253                 :             : 
     254                 :             :     case BTF_KIND_VAR:
     255                 :          18 :       vlen_bytes += sizeof (struct btf_var);
     256                 :             :       break;
     257                 :             : 
     258                 :           0 :     case BTF_KIND_DATASEC:
     259                 :           0 :       vlen_bytes += vlen * sizeof (struct btf_var_secinfo);
     260                 :           0 :       break;
     261                 :             : 
     262                 :             :     case BTF_KIND_DECL_TAG:
     263                 :          18 :       vlen_bytes += sizeof (struct btf_decl_tag);
     264                 :             :       break;
     265                 :             : 
     266                 :             :     default:
     267                 :             :       break;
     268                 :             :     }
     269                 :         426 :   return vlen_bytes;
     270                 :             : }
     271                 :             : 
     272                 :             : /* Initialize BTF section (.BTF) for output.  */
     273                 :             : 
     274                 :             : void
     275                 :         110 : init_btf_sections (void)
     276                 :             : {
     277                 :         110 :   btf_info_section = get_section (BTF_INFO_SECTION_NAME, BTF_INFO_SECTION_FLAGS,
     278                 :             :                                   NULL);
     279                 :             : 
     280                 :         110 :   ASM_GENERATE_INTERNAL_LABEL (btf_info_section_label,
     281                 :             :                                BTF_INFO_SECTION_LABEL, btf_label_num++);
     282                 :         110 : }
     283                 :             : 
     284                 :             : /* Return the section name, as of interest to btf_collect_datasec, for the
     285                 :             :    given symtab node.  Note that this deliberately returns NULL for objects
     286                 :             :    which do not go in a section btf_collect_datasec cares about.  */
     287                 :             : static const char *
     288                 :         101 : get_section_name (symtab_node *node)
     289                 :             : {
     290                 :         101 :   const char *section_name = node->get_section ();
     291                 :             : 
     292                 :           8 :   if (section_name == NULL)
     293                 :             :     {
     294                 :          93 :       switch (categorize_decl_for_section (node->decl, 0))
     295                 :             :         {
     296                 :          59 :         case SECCAT_BSS:
     297                 :          59 :           section_name = ".bss";
     298                 :          59 :           break;
     299                 :          22 :         case SECCAT_DATA:
     300                 :          22 :           section_name = ".data";
     301                 :          22 :           break;
     302                 :          11 :         case SECCAT_RODATA:
     303                 :          11 :           section_name = ".rodata";
     304                 :          11 :           break;
     305                 :             :         default:;
     306                 :             :         }
     307                 :             :     }
     308                 :             : 
     309                 :         101 :   return section_name;
     310                 :             : }
     311                 :             : 
     312                 :             : /* Return true iff DMD is a member description of a bit-field which can be
     313                 :             :    validly represented in BTF.  */
     314                 :             : 
     315                 :             : static bool
     316                 :          86 : btf_dmd_representable_bitfield_p (ctf_dmdef_t *dmd)
     317                 :             : {
     318                 :          86 :   ctf_dtdef_ref ref_type = dmd->dmd_type;
     319                 :          86 :   if (!ref_type)
     320                 :             :     return false;
     321                 :             : 
     322                 :          86 :   if (CTF_V2_INFO_KIND (ref_type->dtd_data.ctti_info) == CTF_K_SLICE)
     323                 :             :     {
     324                 :          14 :       unsigned short word_offset = ref_type->dtd_u.dtu_slice.cts_offset;
     325                 :          14 :       unsigned short bits = ref_type->dtd_u.dtu_slice.cts_bits;
     326                 :          14 :       uint64_t sou_offset = dmd->dmd_offset;
     327                 :             : 
     328                 :          14 :       if ((bits > 0xff) || ((sou_offset + word_offset) > 0xffffff))
     329                 :             :         return false;
     330                 :             : 
     331                 :          12 :       return true;
     332                 :             :     }
     333                 :             : 
     334                 :             :   return false;
     335                 :             : }
     336                 :             : 
     337                 :             : /* BTF asm helper routines.  */
     338                 :             : 
     339                 :             : /* Asm'out a reference to another BTF type.  */
     340                 :             : 
     341                 :             : static void
     342                 :         534 : btf_asm_type_ref (const char *prefix, ctf_dtdef_ref dtd)
     343                 :             : {
     344                 :         534 :   if (!dtd || !btf_emit_type_p (dtd))
     345                 :          13 :     dw2_asm_output_data (4, BTF_VOID_TYPEID, "%s: void", prefix);
     346                 :             :   else
     347                 :             :     {
     348                 :         521 :       uint32_t kind = btf_dtd_kind (dtd);
     349                 :         521 :       if (btf_fwd_to_enum_p (dtd))
     350                 :             :         kind = BTF_KIND_ENUM;
     351                 :         520 :       else if (kind == BTF_KIND_FUNC_PROTO && dtd->dtd_type > max_translated_id)
     352                 :          10 :         kind = BTF_KIND_FUNC;
     353                 :             : 
     354                 :         850 :       dw2_asm_output_data (4, dtd->dtd_type, "%s: (BTF_KIND_%s '%s')",
     355                 :             :                            prefix, btf_kind_name (kind),
     356                 :             :                            get_btf_type_name (dtd));
     357                 :             :     }
     358                 :         534 : }
     359                 :             : 
     360                 :             : /* Asm'out a BTF type. This routine is responsible for the bulk of the task
     361                 :             :    of converting CTF types to their BTF representation.  */
     362                 :             : 
     363                 :             : static void
     364                 :         426 : btf_asm_type (ctf_dtdef_ref dtd)
     365                 :             : {
     366                 :         426 :   uint32_t btf_kind, btf_kflag, btf_vlen, btf_size;
     367                 :         426 :   uint32_t ctf_info = dtd->dtd_data.ctti_info;
     368                 :             : 
     369                 :         426 :   btf_kind = btf_dtd_kind (dtd);
     370                 :         426 :   btf_size = dtd->dtd_data.ctti_size;
     371                 :         426 :   btf_vlen = CTF_V2_INFO_VLEN (ctf_info);
     372                 :             : 
     373                 :             :   /* By now any unrepresentable types have been removed.  */
     374                 :         426 :   gcc_assert (btf_kind != BTF_KIND_UNKN);
     375                 :             : 
     376                 :             :   /* Size 0 integers are redundant definitions of void. None should remain
     377                 :             :      in the types list by this point.  */
     378                 :         426 :   gcc_assert (btf_kind != BTF_KIND_INT || btf_size >= 1);
     379                 :             : 
     380                 :             :   /* Re-encode the ctti_info to BTF.  */
     381                 :             :   /* kflag is 1 for structs/unions with a bitfield member.
     382                 :             :      kflag is 1 for forwards to unions.
     383                 :             :      kflag is 0 in all other cases.  */
     384                 :         426 :   btf_kflag = 0;
     385                 :             : 
     386                 :         426 :   if (btf_kind == BTF_KIND_STRUCT || btf_kind == BTF_KIND_UNION)
     387                 :             :     {
     388                 :             :       /* If a struct/union has ANY bitfield members, set kflag=1.  */
     389                 :          35 :       ctf_dmdef_t *dmd;
     390                 :          35 :       for (dmd = dtd->dtd_u.dtu_members;
     391                 :         108 :            dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
     392                 :             :         {
     393                 :             :           /* Set kflag if this member is a representable bitfield.  */
     394                 :          77 :           if (btf_dmd_representable_bitfield_p (dmd))
     395                 :             :             {
     396                 :             :               btf_kflag = 1;
     397                 :             :               break;
     398                 :             :             }
     399                 :             :         }
     400                 :             :     }
     401                 :             : 
     402                 :             :   /* BTF forwards make use of KIND_FLAG to distinguish between forwards to
     403                 :             :      structs and forwards to unions. The dwarf2ctf conversion process stores
     404                 :             :      the kind of the forward in ctti_type, but for BTF this must be 0 for
     405                 :             :      forwards, with only the KIND_FLAG to distinguish.
     406                 :             :      Forwards to enum types are special-cased below.  */
     407                 :         391 :   else if (btf_kind == BTF_KIND_FWD)
     408                 :             :     {
     409                 :           6 :       if (dtd->dtd_data.ctti_type == CTF_K_UNION)
     410                 :             :         btf_kflag = 1;
     411                 :             : 
     412                 :             :       /* PR debug/111735.  Encode foward-declared enums as BTF_KIND_ENUM
     413                 :             :          with vlen=0.  A representation for these is not formally defined;
     414                 :             :          this is the de-facto standard used by other tools like clang
     415                 :             :          and pahole.  */
     416                 :           5 :       else if (dtd->dtd_data.ctti_type == CTF_K_ENUM)
     417                 :             :         {
     418                 :           1 :           btf_kind = BTF_KIND_ENUM;
     419                 :           1 :           btf_vlen = 0;
     420                 :             :         }
     421                 :             : 
     422                 :             :       btf_size = 0;
     423                 :             :     }
     424                 :             : 
     425                 :         385 :   else if (btf_kind == BTF_KIND_ENUM)
     426                 :             :     {
     427                 :           8 :       btf_kflag = dtd->dtd_enum_unsigned
     428                 :           8 :                     ? BTF_KF_ENUM_UNSIGNED
     429                 :             :                     : BTF_KF_ENUM_SIGNED;
     430                 :           8 :       if (dtd->dtd_data.ctti_size == 0x8)
     431                 :           3 :         btf_kind = BTF_KIND_ENUM64;
     432                 :             :     }
     433                 :             : 
     434                 :             :   /* PR debug/112656.  BTF_KIND_FUNC_PROTO is always anonymous.  */
     435                 :         377 :   else if (btf_kind == BTF_KIND_FUNC_PROTO)
     436                 :          85 :     dtd->dtd_data.ctti_name = 0;
     437                 :             : 
     438                 :         670 :   dw2_asm_output_data (4, dtd->dtd_data.ctti_name,
     439                 :             :                        "TYPE %" PRIu64 " BTF_KIND_%s '%s'",
     440                 :             :                        dtd->dtd_type, btf_kind_name (btf_kind),
     441                 :             :                        get_btf_type_name (dtd));
     442                 :         845 :   dw2_asm_output_data (4, BTF_TYPE_INFO (btf_kind, btf_kflag, btf_vlen),
     443                 :             :                        "btt_info: kind=%u, kflag=%u, vlen=%u",
     444                 :             :                        btf_kind, btf_kflag, btf_vlen);
     445                 :         426 :   switch (btf_kind)
     446                 :             :     {
     447                 :         203 :     case BTF_KIND_INT:
     448                 :         203 :     case BTF_KIND_FLOAT:
     449                 :         203 :     case BTF_KIND_STRUCT:
     450                 :         203 :     case BTF_KIND_UNION:
     451                 :         203 :     case BTF_KIND_ENUM:
     452                 :         203 :     case BTF_KIND_DATASEC:
     453                 :         203 :     case BTF_KIND_ENUM64:
     454                 :         203 :       dw2_asm_output_data (4, btf_size, "btt_size: %uB", btf_size);
     455                 :         203 :       return;
     456                 :          27 :     case BTF_KIND_ARRAY:
     457                 :          27 :     case BTF_KIND_FWD:
     458                 :             :       /* These types do not encode any information in the size/type field
     459                 :             :          and should write 0.  */
     460                 :          27 :       dw2_asm_output_data (4, 0, "(unused)");
     461                 :          27 :       return;
     462                 :          18 :     case BTF_KIND_DECL_TAG:
     463                 :          18 :       {
     464                 :          18 :         if (dtd->ref_type)
     465                 :             :           break;
     466                 :           6 :         else if (dtd->dtd_u.dtu_tag.ref_var)
     467                 :             :           {
     468                 :             :             /* ref_type is NULL for decl tag attached to a variable.  */
     469                 :           6 :             ctf_dvdef_ref dvd = dtd->dtd_u.dtu_tag.ref_var;
     470                 :           6 :             dw2_asm_output_data (4, dvd->dvd_id,
     471                 :             :                                  "btt_type: (BTF_KIND_VAR '%s')",
     472                 :             :                                  dvd->dvd_name);
     473                 :           6 :             return;
     474                 :             :           }
     475                 :             :       }
     476                 :             :     default:
     477                 :             :       break;
     478                 :             :     }
     479                 :             : 
     480                 :         190 :   btf_asm_type_ref ("btt_type", dtd->ref_type);
     481                 :             : }
     482                 :             : 
     483                 :             : /* Asm'out the variable information following a BTF_KIND_ARRAY.  */
     484                 :             : 
     485                 :             : static void
     486                 :          22 : btf_asm_array (ctf_arinfo_t arr)
     487                 :             : {
     488                 :          22 :   btf_asm_type_ref ("bta_elem_type", arr.ctr_contents);
     489                 :          22 :   btf_asm_type_ref ("bta_index_type", arr.ctr_index);
     490                 :          22 :   dw2_asm_output_data (4, arr.ctr_nelems, "bta_nelems");
     491                 :          22 : }
     492                 :             : 
     493                 :             : /* Asm'out a BTF_KIND_VAR.  */
     494                 :             : 
     495                 :             : static void
     496                 :         101 : btf_asm_varent (ctf_dvdef_ref var)
     497                 :             : {
     498                 :         101 :   dw2_asm_output_data (4, var->dvd_name_offset,
     499                 :             :                        "TYPE %" PRIu64 " BTF_KIND_VAR '%s'",
     500                 :             :                        var->dvd_id, var->dvd_name);
     501                 :         101 :   dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_VAR, 0, 0), "btv_info");
     502                 :         101 :   btf_asm_type_ref ("btv_type", var->dvd_type);
     503                 :         101 :   dw2_asm_output_data (4, var->dvd_visibility, "btv_linkage");
     504                 :         101 : }
     505                 :             : 
     506                 :             : /* Asm'out a member description following a BTF_KIND_STRUCT or
     507                 :             :    BTF_KIND_UNION.  */
     508                 :             : 
     509                 :             : static void
     510                 :          81 : btf_asm_sou_member (ctf_dmdef_t * dmd, unsigned int idx)
     511                 :             : {
     512                 :          81 :   ctf_dtdef_ref base_type = dmd->dmd_type;
     513                 :          81 :   uint64_t sou_offset = dmd->dmd_offset;
     514                 :             : 
     515                 :          81 :   dw2_asm_output_data (4, dmd->dmd_name_offset,
     516                 :             :                        "MEMBER '%s' idx=%u",
     517                 :             :                        dmd->dmd_name, idx);
     518                 :             : 
     519                 :          81 :   if (base_type
     520                 :          81 :       && CTF_V2_INFO_KIND (base_type->dtd_data.ctti_info) == CTF_K_SLICE)
     521                 :             :     {
     522                 :           9 :       if (btf_dmd_representable_bitfield_p (dmd))
     523                 :             :         {
     524                 :           8 :           unsigned short word_offset = base_type->dtd_u.dtu_slice.cts_offset;
     525                 :           8 :           unsigned short bits = base_type->dtd_u.dtu_slice.cts_bits;
     526                 :             : 
     527                 :             :           /* Pack the bit offset and bitfield size together.  */
     528                 :           8 :           sou_offset += word_offset;
     529                 :           8 :           sou_offset &= 0x00ffffff;
     530                 :           8 :           sou_offset |= ((bits & 0xff) << 24);
     531                 :             : 
     532                 :             :           /* Refer to the base type of the slice.  */
     533                 :           8 :           base_type = base_type->dtd_u.dtu_slice.cts_type;
     534                 :             :         }
     535                 :             :       else
     536                 :             :         {
     537                 :             :           /* Bitfield cannot be represented in BTF.  Emit the member as having
     538                 :             :              'void' type.  */
     539                 :             :           base_type = NULL;
     540                 :             :         }
     541                 :             :     }
     542                 :             : 
     543                 :          81 :   btf_asm_type_ref ("btm_type", base_type);
     544                 :          81 :   dw2_asm_output_data (4, sou_offset, "btm_offset");
     545                 :          81 : }
     546                 :             : 
     547                 :             : /* Asm'out an enum constant following a BTF_KIND_ENUM{,64}.  */
     548                 :             : 
     549                 :             : static void
     550                 :          25 : btf_asm_enum_const (unsigned int size, ctf_dmdef_t * dmd, unsigned int idx)
     551                 :             : {
     552                 :          25 :   dw2_asm_output_data (4, dmd->dmd_name_offset, "ENUM_CONST '%s' idx=%u",
     553                 :             :                        dmd->dmd_name, idx);
     554                 :          25 :   if (size <= 4)
     555                 :          16 :     dw2_asm_output_data (size < 4 ? 4 : size, dmd->dmd_value, "bte_value");
     556                 :             :   else
     557                 :             :     {
     558                 :           9 :       dw2_asm_output_data (4, dmd->dmd_value & 0xffffffff, "bte_value_lo32");
     559                 :           9 :       dw2_asm_output_data (4, (dmd->dmd_value >> 32) & 0xffffffff, "bte_value_hi32");
     560                 :             :     }
     561                 :          25 : }
     562                 :             : 
     563                 :             : /* Asm'out a function parameter description following a BTF_KIND_FUNC_PROTO.  */
     564                 :             : 
     565                 :             : static void
     566                 :          32 : btf_asm_func_arg (ctf_func_arg_t * farg, size_t stroffset)
     567                 :             : {
     568                 :             :   /* If the function arg does not have a name, refer to the null string at
     569                 :             :      the start of the string table. This ensures correct encoding for varargs
     570                 :             :      '...' arguments.  */
     571                 :          32 :   if ((farg->farg_name != NULL) && strcmp (farg->farg_name, ""))
     572                 :          21 :     dw2_asm_output_data (4, farg->farg_name_offset + stroffset,
     573                 :             :                          "farg_name '%s'", farg->farg_name);
     574                 :             :   else
     575                 :          11 :     dw2_asm_output_data (4, 0, "farg_name ''");
     576                 :             : 
     577                 :          32 :   btf_asm_type_ref ("farg_type", farg->farg_type);
     578                 :          32 : }
     579                 :             : 
     580                 :             : /* Asm'out a BTF_KIND_FUNC type.  */
     581                 :             : 
     582                 :             : static void
     583                 :          83 : btf_asm_func_type (ctf_dtdef_ref dtd)
     584                 :             : {
     585                 :         166 :   dw2_asm_output_data (4, dtd->dtd_data.ctti_name,
     586                 :             :                        "TYPE %" PRIu64 " BTF_KIND_FUNC '%s'",
     587                 :             :                        dtd->dtd_type, get_btf_type_name (dtd));
     588                 :          83 :   dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_FUNC, 0, dtd->linkage),
     589                 :             :                        "btt_info: kind=%u, kflag=%u, linkage=%u",
     590                 :             :                        BTF_KIND_FUNC, 0, dtd->linkage);
     591                 :          83 :   btf_asm_type_ref ("btt_type", dtd->ref_type);
     592                 :          83 : }
     593                 :             : 
     594                 :             : /* Asm'out a variable entry following a BTF_KIND_DATASEC.  */
     595                 :             : 
     596                 :             : static void
     597                 :         100 : btf_asm_datasec_entry (struct btf_datasec_entry entry)
     598                 :             : {
     599                 :         100 :   const char *symbol_name = NULL;
     600                 :         100 :   if (entry.is_var)
     601                 :             :     {
     602                 :          97 :       symbol_name = entry.dvd->dvd_name;
     603                 :          97 :       dw2_asm_output_data (4, entry.dvd->dvd_id,
     604                 :             :                            "bts_type: (BTF_KIND_VAR '%s')", symbol_name);
     605                 :             :     }
     606                 :             :   else
     607                 :             :     {
     608                 :           3 :       symbol_name = entry.dtd->dtd_name;
     609                 :           3 :       btf_asm_type_ref ("bts_type", entry.dtd);
     610                 :             :     }
     611                 :             : 
     612                 :         100 :   if (!btf_with_core_debuginfo_p () || symbol_name == NULL)
     613                 :         100 :     dw2_asm_output_data (4, 0, "bts_offset");
     614                 :             :   else
     615                 :           0 :     dw2_asm_output_offset (4, symbol_name, NULL, "bts_offset");
     616                 :             : 
     617                 :         100 :   dw2_asm_output_data (4, entry.size, "bts_size");
     618                 :         100 : }
     619                 :             : 
     620                 :             : /* Asm'out a whole BTF_KIND_DATASEC, including its variable entries.  */
     621                 :             : 
     622                 :             : static void
     623                 :          52 : btf_asm_datasec_type (btf_datasec_t ds)
     624                 :             : {
     625                 :          52 :   dw2_asm_output_data (4, ds.name_offset,
     626                 :             :                        "TYPE %" PRIu64 " BTF_KIND_DATASEC '%s'",
     627                 :             :                        ds.id, ds.name);
     628                 :         104 :   dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_DATASEC, 0,
     629                 :             :                                          ds.entries.length ()),
     630                 :             :                        "btt_info: n_entries=%u", ds.entries.length ());
     631                 :             :   /* Note: the "total section size in bytes" is emitted as 0 and patched by
     632                 :             :      loaders such as libbpf.  */
     633                 :          52 :   dw2_asm_output_data (4, 0, "btt_size");
     634                 :         152 :   for (size_t i = 0; i < ds.entries.length (); i++)
     635                 :         100 :     btf_asm_datasec_entry (ds.entries[i]);
     636                 :          52 : }
     637                 :             : 
     638                 :             : /* Compute and output the header information for a .BTF section.  */
     639                 :             : 
     640                 :             : static void
     641                 :         110 : output_btf_header (ctf_container_ref ctfc)
     642                 :             : {
     643                 :         110 :    switch_to_section (btf_info_section);
     644                 :         110 :    ASM_OUTPUT_LABEL (asm_out_file, btf_info_section_label);
     645                 :             : 
     646                 :             :    /* BTF magic number, version, flags, and header length.  */
     647                 :         110 :    dw2_asm_output_data (2, BTF_MAGIC, "btf_magic");
     648                 :         110 :    dw2_asm_output_data (1, BTF_VERSION, "btf_version");
     649                 :         110 :    dw2_asm_output_data (1, 0, "btf_flags");
     650                 :         110 :    dw2_asm_output_data (4, sizeof (struct btf_header), "btf_hdr_len");
     651                 :             : 
     652                 :         110 :    uint32_t type_off = 0, type_len = 0;
     653                 :         110 :    uint32_t str_off = 0, str_len = 0;
     654                 :             : 
     655                 :         110 :    if (!ctfc_is_empty_container (ctfc))
     656                 :             :      {
     657                 :             :        /* Total length (bytes) of the types section.  */
     658                 :         110 :        type_len = ctfc->ctfc_num_types * sizeof (struct btf_type)
     659                 :         110 :          + ctfc->ctfc_num_vlen_bytes;
     660                 :             : 
     661                 :         110 :        str_off = type_off + type_len;
     662                 :             : 
     663                 :         110 :        str_len = ctfc->ctfc_strtable.ctstab_len
     664                 :         110 :          + ctfc->ctfc_aux_strtable.ctstab_len;
     665                 :             :      }
     666                 :             : 
     667                 :             :    /* Offset of type section.  */
     668                 :         110 :    dw2_asm_output_data (4, type_off, "type_off");
     669                 :             :    /* Length of type section in bytes.  */
     670                 :         110 :    dw2_asm_output_data (4, type_len, "type_len: ntypes=%u, vlen=%u",
     671                 :         110 :                         (uint32_t) ctfc->ctfc_num_types,
     672                 :         110 :                         (uint32_t) ctfc->ctfc_num_vlen_bytes);
     673                 :             :     /* Offset of string section.  */
     674                 :         110 :    dw2_asm_output_data (4, str_off, "str_off");
     675                 :             :     /* Length of string section in bytes.  */
     676                 :         110 :    dw2_asm_output_data (4, str_len, "str_len");
     677                 :         110 : }
     678                 :             : 
     679                 :             : /* Output all BTF_KIND_VARs in CTFC.  */
     680                 :             : 
     681                 :             : static void
     682                 :         110 : output_btf_vars (ctf_container_ref ctfc)
     683                 :             : {
     684                 :         110 :   size_t i;
     685                 :         110 :   size_t num_ctf_vars = ctfc->ctfc_vars_list_count;
     686                 :         110 :   if (num_ctf_vars)
     687                 :             :     {
     688                 :         139 :       for (i = 0; i < num_ctf_vars; i++)
     689                 :         101 :         btf_asm_varent (ctfc->ctfc_vars_list[i]);
     690                 :             :     }
     691                 :         110 : }
     692                 :             : 
     693                 :             : /* Output BTF string records. The BTF strings section is a concatenation
     694                 :             :    of the standard and auxilliary string tables in the ctf container.  */
     695                 :             : 
     696                 :             : static void
     697                 :         110 : output_btf_strs (ctf_container_ref ctfc)
     698                 :             : {
     699                 :         110 :   ctf_string_t * ctf_string = ctfc->ctfc_strtable.ctstab_head;
     700                 :         110 :   static int str_pos = 0;
     701                 :             : 
     702                 :         742 :   while (ctf_string)
     703                 :             :     {
     704                 :         632 :       dw2_asm_output_nstring (ctf_string->cts_str, -1,
     705                 :             :                               "btf_string, str_pos = 0x%x", str_pos);
     706                 :         632 :       str_pos += strlen(ctf_string->cts_str) + 1;
     707                 :         632 :       ctf_string = ctf_string->cts_next;
     708                 :             :     }
     709                 :             : 
     710                 :         110 :   ctf_string = ctfc->ctfc_aux_strtable.ctstab_head;
     711                 :         325 :   while (ctf_string)
     712                 :             :     {
     713                 :         215 :       dw2_asm_output_nstring (ctf_string->cts_str, -1,
     714                 :             :                               "btf_aux_string, str_pos = 0x%x", str_pos);
     715                 :         215 :       str_pos += strlen(ctf_string->cts_str) + 1;
     716                 :         215 :       ctf_string = ctf_string->cts_next;
     717                 :             :     }
     718                 :         110 : }
     719                 :             : 
     720                 :             : /* Output all (representable) members of a BTF_KIND_STRUCT or
     721                 :             :    BTF_KIND_UNION type.  */
     722                 :             : 
     723                 :             : static void
     724                 :          35 : output_asm_btf_sou_fields (ctf_dtdef_ref dtd)
     725                 :             : {
     726                 :          35 :   ctf_dmdef_t * dmd;
     727                 :             : 
     728                 :          35 :   unsigned idx = 0;
     729                 :          35 :   for (dmd = dtd->dtd_u.dtu_members;
     730                 :         116 :        dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
     731                 :             :     {
     732                 :          81 :       btf_asm_sou_member (dmd, idx);
     733                 :          81 :       idx++;
     734                 :             :     }
     735                 :          35 : }
     736                 :             : 
     737                 :             : /* Output all enumerator constants following a BTF_KIND_ENUM{,64}.  */
     738                 :             : 
     739                 :             : static void
     740                 :           8 : output_asm_btf_enum_list (ctf_dtdef_ref dtd)
     741                 :             : {
     742                 :           8 :   ctf_dmdef_t * dmd;
     743                 :             : 
     744                 :           8 :   unsigned idx = 0;
     745                 :           8 :   for (dmd = dtd->dtd_u.dtu_members;
     746                 :          33 :        dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
     747                 :             :     {
     748                 :          25 :       btf_asm_enum_const (dtd->dtd_data.ctti_size, dmd, idx);
     749                 :          25 :       idx++;
     750                 :             :     }
     751                 :           8 : }
     752                 :             : 
     753                 :             : /* Output all function arguments following a BTF_KIND_FUNC_PROTO.  */
     754                 :             : 
     755                 :             : static void
     756                 :          85 : output_asm_btf_func_args_list (ctf_container_ref ctfc,
     757                 :             :                                ctf_dtdef_ref dtd)
     758                 :             : {
     759                 :          85 :   size_t farg_name_offset = ctfc_get_strtab_len (ctfc, CTF_STRTAB);
     760                 :          85 :   ctf_func_arg_t * farg;
     761                 :          85 :   for (farg = dtd->dtd_u.dtu_argv;
     762                 :         117 :        farg != NULL; farg = (ctf_func_arg_t *) ctf_farg_list_next (farg))
     763                 :          32 :     btf_asm_func_arg (farg, farg_name_offset);
     764                 :          85 : }
     765                 :             : 
     766                 :             : /* Output the variable portion of a BTF type record. The information depends
     767                 :             :    on the kind of the type.  */
     768                 :             : 
     769                 :             : static void
     770                 :         426 : output_asm_btf_vlen_bytes (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
     771                 :             : {
     772                 :         426 :   uint32_t btf_kind, encoding;
     773                 :             : 
     774                 :         426 :   btf_kind = btf_dtd_kind (dtd);
     775                 :             : 
     776                 :         426 :   if (btf_kind == BTF_KIND_UNKN)
     777                 :             :     return;
     778                 :             : 
     779                 :         426 :   switch (btf_kind)
     780                 :             :     {
     781                 :         153 :     case BTF_KIND_INT:
     782                 :             :       /* Redundant definitions of void may still be hanging around in the type
     783                 :             :          list as size 0 integers. Skip emitting them.  */
     784                 :         153 :       if (dtd->dtd_data.ctti_size < 1)
     785                 :             :         break;
     786                 :             : 
     787                 :             :       /* In BTF the CHAR `encoding' seems to not be used, so clear it here.  */
     788                 :         153 :       dtd->dtd_u.dtu_enc.cte_format &= ~BTF_INT_CHAR;
     789                 :             : 
     790                 :         153 :       encoding = BTF_INT_DATA (dtd->dtd_u.dtu_enc.cte_format,
     791                 :             :                                dtd->dtd_u.dtu_enc.cte_offset,
     792                 :             :                                dtd->dtd_u.dtu_enc.cte_bits);
     793                 :             : 
     794                 :         153 :       dw2_asm_output_data (4, encoding, "bti_encoding");
     795                 :         153 :       break;
     796                 :             : 
     797                 :          22 :     case BTF_KIND_ARRAY:
     798                 :          22 :       btf_asm_array (dtd->dtd_u.dtu_arr);
     799                 :          22 :       break;
     800                 :             : 
     801                 :          35 :     case BTF_KIND_STRUCT:
     802                 :          35 :     case BTF_KIND_UNION:
     803                 :          35 :       output_asm_btf_sou_fields (dtd);
     804                 :          35 :       break;
     805                 :             : 
     806                 :           8 :     case BTF_KIND_ENUM:
     807                 :           8 :       output_asm_btf_enum_list (dtd);
     808                 :           8 :       break;
     809                 :             : 
     810                 :          85 :     case BTF_KIND_FUNC_PROTO:
     811                 :          85 :       output_asm_btf_func_args_list (ctfc, dtd);
     812                 :          85 :       break;
     813                 :             : 
     814                 :           0 :     case BTF_KIND_VAR:
     815                 :             :       /* BTF Variables are handled by output_btf_vars and btf_asm_varent.
     816                 :             :          There should be no BTF_KIND_VAR types at this point.  */
     817                 :           0 :       gcc_unreachable ();
     818                 :             : 
     819                 :           0 :     case BTF_KIND_DATASEC:
     820                 :             :       /* The BTF_KIND_DATASEC records are handled by output_btf_datasec_types
     821                 :             :          and btf_asm_datasec_type. There should be no BTF_KIND_DATASEC types
     822                 :             :          at this point.  */
     823                 :           0 :       gcc_unreachable ();
     824                 :             : 
     825                 :          18 :     case BTF_KIND_DECL_TAG:
     826                 :          18 :       dw2_asm_output_data (4, dtd->dtd_u.dtu_tag.component_idx,
     827                 :             :                            "component_idx=%d",
     828                 :             :                            dtd->dtd_u.dtu_tag.component_idx);
     829                 :          18 :       break;
     830                 :             : 
     831                 :             :     default:
     832                 :             :       /* All other BTF type kinds have no variable length data.  */
     833                 :             :       break;
     834                 :             :     }
     835                 :             : }
     836                 :             : 
     837                 :             : /* Output a whole BTF type record for TYPE, including the fixed and variable
     838                 :             :    data portions.  */
     839                 :             : 
     840                 :             : static void
     841                 :         445 : output_asm_btf_type (ctf_container_ref ctfc, ctf_dtdef_ref type)
     842                 :             : {
     843                 :         445 :   if (btf_emit_type_p (type))
     844                 :             :     {
     845                 :         426 :       btf_asm_type (type);
     846                 :         426 :       output_asm_btf_vlen_bytes (ctfc, type);
     847                 :             :     }
     848                 :         445 : }
     849                 :             : 
     850                 :             : /* Output all BTF types in the container. This does not include synthesized
     851                 :             :    types: BTF_KIND_VAR, BTF_KIND_FUNC, nor BTF_KIND_DATASEC.  */
     852                 :             : 
     853                 :             : static void
     854                 :         110 : output_btf_types (ctf_container_ref ctfc)
     855                 :             : {
     856                 :         110 :   size_t i;
     857                 :         110 :   size_t num_types;
     858                 :         110 :   if (debug_prune_btf)
     859                 :           5 :     num_types = max_translated_id;
     860                 :             :   else
     861                 :         105 :     num_types = ctfc->ctfc_types->elements ();
     862                 :             : 
     863                 :         110 :   if (num_types)
     864                 :             :     {
     865                 :         529 :       for (i = 1; i <= num_types; i++)
     866                 :         419 :         output_asm_btf_type (ctfc, ctfc->ctfc_types_list[i]);
     867                 :             :     }
     868                 :         110 : }
     869                 :             : 
     870                 :             : /* Output all BTF_KIND_FUNC type records.  */
     871                 :             : 
     872                 :             : static void
     873                 :         110 : output_btf_func_types (void)
     874                 :             : {
     875                 :         110 :   ctf_dtdef_ref ref;
     876                 :         110 :   unsigned i;
     877                 :         193 :   FOR_EACH_VEC_ELT (*funcs, i, ref)
     878                 :          83 :     btf_asm_func_type (ref);
     879                 :         110 : }
     880                 :             : 
     881                 :             : static void
     882                 :         110 : output_btf_tags (ctf_container_ref ctfc)
     883                 :             : {
     884                 :             :   /* If pruning, tags which are not pruned have already been added to
     885                 :             :      the used list and output by output_btf_types.  */
     886                 :         110 :   if (debug_prune_btf)
     887                 :         110 :     return;
     888                 :             : 
     889                 :             :   ctf_dtdef_ref dtd;
     890                 :             :   unsigned i;
     891                 :         131 :   FOR_EACH_VEC_ELT (*ctfc->ctfc_tags, i, dtd)
     892                 :          26 :     output_asm_btf_type (ctfc, dtd);
     893                 :             : }
     894                 :             : 
     895                 :             : /* Output all BTF_KIND_DATASEC records.  */
     896                 :             : 
     897                 :             : static void
     898                 :         110 : output_btf_datasec_types (void)
     899                 :             : {
     900                 :         162 :   for (size_t i = 0; i < datasecs.length (); i++)
     901                 :          52 :     btf_asm_datasec_type (datasecs[i]);
     902                 :         110 : }
     903                 :             : 
     904                 :             : /* Write out all BTF debug info.  */
     905                 :             : 
     906                 :             : void
     907                 :         110 : btf_output (ctf_container_ref ctfc)
     908                 :             : {
     909                 :         110 :   output_btf_header (ctfc);
     910                 :         110 :   output_btf_types (ctfc);
     911                 :         110 :   output_btf_vars (ctfc);
     912                 :         110 :   output_btf_func_types ();
     913                 :         110 :   output_btf_tags (ctfc);
     914                 :         110 :   output_btf_datasec_types ();
     915                 :         110 :   output_btf_strs (ctfc);
     916                 :         110 : }
     917                 :             : 
     918                 :             : /* Workaround for 'const void' variables.  These variables are sometimes used
     919                 :             :    in eBPF programs to address kernel symbols.  DWARF does not generate const
     920                 :             :    qualifier on void type, so we would incorrectly emit these variables
     921                 :             :    without the const qualifier.  Find any such variables, and update them to
     922                 :             :    refer to a new 'const' modifier type for void.  */
     923                 :             : 
     924                 :             : static void
     925                 :         110 : btf_add_const_void (ctf_container_ref ctfc)
     926                 :             : {
     927                 :         110 :   ctf_dtdef_ref constvoid_dtd = NULL;
     928                 :         110 :   varpool_node *var;
     929                 :         424 :   FOR_EACH_VARIABLE (var)
     930                 :             :     {
     931                 :         102 :       if (!var->decl)
     932                 :           0 :         continue;
     933                 :             : 
     934                 :         102 :       tree type = TREE_TYPE (var->decl);
     935                 :         102 :       if (type && VOID_TYPE_P (type) && TYPE_READONLY (type))
     936                 :             :         {
     937                 :           1 :           dw_die_ref die = lookup_decl_die (var->decl);
     938                 :           1 :           if (die == NULL)
     939                 :           0 :             continue;
     940                 :             : 
     941                 :           1 :           ctf_dvdef_ref dvd = ctf_dvd_lookup (ctfc, die);
     942                 :           1 :           if (dvd == NULL)
     943                 :           0 :             continue;
     944                 :             : 
     945                 :             :           /* Create the 'const' modifier type for void.  */
     946                 :           1 :           if (constvoid_dtd == NULL)
     947                 :           1 :             constvoid_dtd = ctf_add_reftype (ctfc, CTF_ADD_ROOT,
     948                 :             :                                              dvd->dvd_type, CTF_K_CONST, NULL);
     949                 :           1 :           dvd->dvd_type = constvoid_dtd;
     950                 :             :         }
     951                 :             :     }
     952                 :         110 : }
     953                 :             : 
     954                 :             : /* Functions actually get two type records: a BTF_KIND_FUNC_PROTO, and also a
     955                 :             :    BTF_KIND_FUNC.  But the CTF container only allocates one type per function,
     956                 :             :    which matches closely with BTF_KIND_FUNC_PROTO.  For each such function,
     957                 :             :    construct a BTF_KIND_FUNC entry.  This is done early, because we want FUNC
     958                 :             :    records even for functions which are later inlined by optimizations.  */
     959                 :             : 
     960                 :             : static void
     961                 :         110 : btf_add_func_records (ctf_container_ref ctfc)
     962                 :             : {
     963                 :         110 :   cgraph_node *func;
     964                 :         386 :   FOR_EACH_FUNCTION (func)
     965                 :             :     {
     966                 :          83 :       dw_die_ref die = lookup_decl_die (func->decl);
     967                 :          83 :       if (die == NULL)
     968                 :           0 :         continue;
     969                 :             : 
     970                 :          83 :       ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, die);
     971                 :          83 :       if (dtd == NULL)
     972                 :           0 :         continue;
     973                 :             : 
     974                 :             :       /* Do not add FUNC records for kernel helpers.  */
     975                 :          83 :       if (DECL_EXTERNAL (func->decl)
     976                 :          87 :           && (lookup_attribute ("kernel_helper",
     977                 :           4 :                                 DECL_ATTRIBUTES (func->decl))) != NULL_TREE)
     978                 :           0 :         continue;
     979                 :             : 
     980                 :          83 :       ctf_dtdef_ref func_dtd = ggc_cleared_alloc<ctf_dtdef_t> ();
     981                 :          83 :       func_dtd->dtd_data = dtd->dtd_data;
     982                 :          83 :       func_dtd->dtd_data.ctti_type = dtd->dtd_type;
     983                 :          83 :       func_dtd->ref_type = dtd;
     984                 :          83 :       func_dtd->linkage = dtd->linkage;
     985                 :          83 :       func_dtd->dtd_name = dtd->dtd_name;
     986                 :             :       /* Type ID will be assigned just before output.  */
     987                 :             : 
     988                 :             :       /* Only the BTF_KIND_FUNC type actually references the name.
     989                 :             :          The BTF_KIND_FUNC_PROTO is always anonymous.  */
     990                 :          83 :       dtd->dtd_data.ctti_name = 0;
     991                 :             : 
     992                 :             :       /* Mark 'extern' funcs.  */
     993                 :          83 :       if (DECL_EXTERNAL (func->decl))
     994                 :           4 :         func_dtd->linkage = BTF_FUNC_EXTERN;
     995                 :             : 
     996                 :             :       /* Buffer newly created FUNC records.  We cannot simply insert them
     997                 :             :          into the types map, because types are keyed by their DWARF DIE,
     998                 :             :          and we have no unique DIE to use as a key since the FUNC_PROTOs
     999                 :             :          are already present in the map.  */
    1000                 :          83 :       vec_safe_push (funcs, func_dtd);
    1001                 :          83 :       func_map->put (dtd, func_dtd);
    1002                 :             :     }
    1003                 :         110 : }
    1004                 :             : 
    1005                 :             : /* The set of types used directly in the source program, and any types manually
    1006                 :             :    marked as used.  This is the set of types which will be emitted when
    1007                 :             :    flag_prune_btf is set.  */
    1008                 :             : static GTY (()) hash_set<ctf_dtdef_ref> *btf_used_types;
    1009                 :             : 
    1010                 :             : /* Fixup used to avoid unnecessary pointer chasing for types.  A fixup is
    1011                 :             :    created when a structure or union member is a pointer to another struct
    1012                 :             :    or union type.  In such cases, avoid emitting full type information for
    1013                 :             :    the pointee struct or union type (which may be quite large), unless that
    1014                 :             :    type is used directly elsewhere.  */
    1015                 :             : struct btf_fixup
    1016                 :             : {
    1017                 :             :   ctf_dtdef_ref pointer_dtd; /* Type node to which the fixup is applied.  */
    1018                 :             :   ctf_dtdef_ref pointee_dtd; /* Original type node referred to by pointer_dtd.
    1019                 :             :                                 If this concrete type is not otherwise used,
    1020                 :             :                                 then a forward is created.  */
    1021                 :             : };
    1022                 :             : 
    1023                 :             : /* Stores fixups while processing types.  */
    1024                 :             : static vec<struct btf_fixup> fixups;
    1025                 :             : 
    1026                 :             : /* For fixups where the underlying type is not used in the end, a BTF_KIND_FWD
    1027                 :             :    is created and emitted.  This vector stores them.  */
    1028                 :             : static GTY (()) vec<ctf_dtdef_ref, va_gc> *forwards;
    1029                 :             : 
    1030                 :             : /* Implementation of btf_add_used_type.
    1031                 :             :    Recursively add type DTD and any types it references to the used set.
    1032                 :             :    Return a type that should be used for references to DTD - usually DTD itself,
    1033                 :             :    but may be NULL if DTD corresponds to a type which will not be emitted.
    1034                 :             :    CHECK_PTR is true if one of the predecessors in recursive calls is a struct
    1035                 :             :    or union member.  SEEN_PTR is true if CHECK_PTR is true AND one of the
    1036                 :             :    predecessors was a pointer type.  These two flags are used to avoid chasing
    1037                 :             :    pointers to struct/union only used from pointer members.  For such types, we
    1038                 :             :    will emit a forward instead of the full type information, unless
    1039                 :             :    CREATE_FIXUPS is false.  */
    1040                 :             : 
    1041                 :             : static ctf_dtdef_ref
    1042                 :          67 : btf_add_used_type_1 (ctf_container_ref ctfc, ctf_dtdef_ref dtd,
    1043                 :             :                      bool check_ptr, bool seen_ptr, bool create_fixups)
    1044                 :             : {
    1045                 :          67 :   if (dtd == NULL)
    1046                 :             :     return NULL;
    1047                 :             : 
    1048                 :          67 :   uint32_t ctf_kind = CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info);
    1049                 :          67 :   uint32_t kind = get_btf_kind (ctf_kind);
    1050                 :             : 
    1051                 :             :   /* Check whether the type has already been added.  */
    1052                 :          67 :   if (btf_used_types->contains (dtd))
    1053                 :             :     {
    1054                 :             :       /* It's possible the type was already added as a fixup, but that we now
    1055                 :             :          have a concrete use of it.  */
    1056                 :          16 :       switch (kind)
    1057                 :             :         {
    1058                 :           5 :         case BTF_KIND_PTR:
    1059                 :           5 :         case BTF_KIND_TYPEDEF:
    1060                 :           5 :         case BTF_KIND_CONST:
    1061                 :           5 :         case BTF_KIND_VOLATILE:
    1062                 :           5 :         case BTF_KIND_RESTRICT:
    1063                 :           5 :           if (check_ptr)
    1064                 :             :             /* Type was previously added as a fixup, and that's OK.  */
    1065                 :             :             return dtd;
    1066                 :             :           else
    1067                 :             :             {
    1068                 :             :               /* The type was previously added as a fixup, but now we have
    1069                 :             :                  a concrete use of it.  Remove the fixup.  */
    1070                 :           3 :               for (size_t i = 0; i < fixups.length (); i++)
    1071                 :           1 :                 if (fixups[i].pointer_dtd == dtd)
    1072                 :           1 :                   fixups.unordered_remove (i);
    1073                 :             : 
    1074                 :             :               /* Add the concrete base type.  */
    1075                 :           2 :               dtd->ref_type = btf_add_used_type_1 (ctfc, dtd->ref_type,
    1076                 :             :                                                    check_ptr, seen_ptr,
    1077                 :             :                                                    create_fixups);
    1078                 :           2 :               return dtd;
    1079                 :             :             }
    1080                 :             :         default:
    1081                 :             :           return dtd;
    1082                 :             :         }
    1083                 :             :     }
    1084                 :             : 
    1085                 :          51 :   if (ctf_kind == CTF_K_SLICE)
    1086                 :             :     {
    1087                 :             :       /* Bitfield.  Add the underlying type to the used set, but leave
    1088                 :             :          the reference to the bitfield.  The slice type won't be emitted,
    1089                 :             :          but we need the information in it when writing out the bitfield
    1090                 :             :          encoding.  */
    1091                 :           0 :       btf_add_used_type_1 (ctfc, dtd->dtd_u.dtu_slice.cts_type,
    1092                 :             :                            check_ptr, seen_ptr, create_fixups);
    1093                 :           0 :       return dtd;
    1094                 :             :     }
    1095                 :             : 
    1096                 :             :   /* Skip redundant definitions of void and types with no BTF encoding.  */
    1097                 :          51 :   if ((kind == BTF_KIND_INT && dtd->dtd_data.ctti_size == 0)
    1098                 :          50 :       || (kind == BTF_KIND_UNKN))
    1099                 :             :     return NULL;
    1100                 :             : 
    1101                 :             :   /* Add the type itself, and assign its id.
    1102                 :             :      Do this before recursing to handle things like linked list structures.  */
    1103                 :          50 :   gcc_assert (ctfc->ctfc_nextid <= BTF_MAX_TYPE);
    1104                 :          50 :   dtd->dtd_type = ctfc->ctfc_nextid++;
    1105                 :          50 :   btf_used_types->add (dtd);
    1106                 :          50 :   ctf_add_string (ctfc, dtd->dtd_name, &(dtd->dtd_data.ctti_name), CTF_STRTAB);
    1107                 :          50 :   ctfc->ctfc_num_types++;
    1108                 :          50 :   ctfc->ctfc_num_vlen_bytes += btf_calc_num_vbytes (dtd);
    1109                 :             : 
    1110                 :             :   /* Recursively add types referenced by this type.  */
    1111                 :          50 :   switch (kind)
    1112                 :             :     {
    1113                 :             :     case BTF_KIND_INT:
    1114                 :             :     case BTF_KIND_FLOAT:
    1115                 :             :     case BTF_KIND_FWD:
    1116                 :             :     case BTF_KIND_DECL_TAG:
    1117                 :             :       /* Leaf kinds which do not refer to any other types.
    1118                 :             :          BTF_KIND_DECL_TAG is a special case: we treat it as though it does not
    1119                 :             :          refer to any other types, since we only want the DECL_TAG to be added
    1120                 :             :          if the type to which it refers has already been added.  */
    1121                 :             :       break;
    1122                 :             : 
    1123                 :           0 :     case BTF_KIND_FUNC:
    1124                 :           0 :     case BTF_KIND_VAR:
    1125                 :             :       /* Root kinds; no type we are visiting may refer to these.  */
    1126                 :           0 :       gcc_unreachable ();
    1127                 :             : 
    1128                 :          17 :     case BTF_KIND_PTR:
    1129                 :          17 :     case BTF_KIND_TYPEDEF:
    1130                 :          17 :     case BTF_KIND_CONST:
    1131                 :          17 :     case BTF_KIND_VOLATILE:
    1132                 :          17 :     case BTF_KIND_RESTRICT:
    1133                 :          17 :     case BTF_KIND_TYPE_TAG:
    1134                 :          17 :       {
    1135                 :             :         /* These type kinds refer to exactly one other type.  */
    1136                 :          17 :         if (check_ptr && !seen_ptr)
    1137                 :           6 :           seen_ptr = (kind == BTF_KIND_PTR);
    1138                 :             : 
    1139                 :             :         /* Try to avoid chasing pointers to struct/union types if the
    1140                 :             :            underlying type isn't used.  */
    1141                 :          17 :         if (check_ptr && seen_ptr && create_fixups)
    1142                 :             :           {
    1143                 :           7 :             ctf_dtdef_ref ref = dtd->ref_type;
    1144                 :           7 :             uint32_t ref_kind = btf_dtd_kind (ref);
    1145                 :             : 
    1146                 :           7 :             if ((ref_kind == BTF_KIND_STRUCT || ref_kind == BTF_KIND_UNION)
    1147                 :           7 :                 && !btf_used_types->contains (ref))
    1148                 :             :               {
    1149                 :           3 :                 struct btf_fixup fixup;
    1150                 :           3 :                 fixup.pointer_dtd = dtd;
    1151                 :           3 :                 fixup.pointee_dtd = ref;
    1152                 :           3 :                 fixups.safe_push (fixup);
    1153                 :           3 :                 break;
    1154                 :             :               }
    1155                 :             :           }
    1156                 :             : 
    1157                 :             :         /* Add the type to which this type refers.  */
    1158                 :          14 :         dtd->ref_type = btf_add_used_type_1 (ctfc, dtd->ref_type, check_ptr,
    1159                 :             :                                              seen_ptr, create_fixups);
    1160                 :          14 :         break;
    1161                 :             :       }
    1162                 :           2 :     case BTF_KIND_ARRAY:
    1163                 :           2 :       {
    1164                 :             :         /* Add element and index types.  */
    1165                 :           2 :         ctf_arinfo_t *arr = &(dtd->dtd_u.dtu_arr);
    1166                 :           2 :         arr->ctr_contents = btf_add_used_type_1 (ctfc, arr->ctr_contents,
    1167                 :             :                                                  false, false, create_fixups);
    1168                 :           2 :         arr->ctr_index = btf_add_used_type_1 (ctfc, arr->ctr_index, false,
    1169                 :             :                                               false, create_fixups);
    1170                 :           2 :         break;
    1171                 :             :       }
    1172                 :           8 :     case BTF_KIND_STRUCT:
    1173                 :           8 :     case BTF_KIND_UNION:
    1174                 :           8 :     case BTF_KIND_ENUM:
    1175                 :           8 :     case BTF_KIND_ENUM64:
    1176                 :           8 :       {
    1177                 :             :         /* Add members.  */
    1178                 :           8 :         ctf_dmdef_t *dmd;
    1179                 :           8 :         for (dmd = dtd->dtd_u.dtu_members;
    1180                 :          27 :              dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
    1181                 :             :           {
    1182                 :             :             /* Add member type for struct/union members.  For enums, only the
    1183                 :             :                enumerator names are needed.  */
    1184                 :          19 :             if (kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION)
    1185                 :          19 :               dmd->dmd_type = btf_add_used_type_1 (ctfc, dmd->dmd_type, true,
    1186                 :             :                                                    false, create_fixups);
    1187                 :          19 :             ctf_add_string (ctfc, dmd->dmd_name, &(dmd->dmd_name_offset),
    1188                 :             :                             CTF_STRTAB);
    1189                 :             :           }
    1190                 :             :         break;
    1191                 :             :       }
    1192                 :           5 :     case BTF_KIND_FUNC_PROTO:
    1193                 :           5 :       {
    1194                 :             :         /* Add return type.  */
    1195                 :           5 :         dtd->ref_type = btf_add_used_type_1 (ctfc, dtd->ref_type, false, false,
    1196                 :             :                                              create_fixups);
    1197                 :             : 
    1198                 :             :         /* Add arg types.  */
    1199                 :           5 :         ctf_func_arg_t * farg;
    1200                 :           5 :         for (farg = dtd->dtd_u.dtu_argv;
    1201                 :          15 :              farg != NULL; farg = (ctf_func_arg_t *) ctf_farg_list_next (farg))
    1202                 :             :           {
    1203                 :          10 :             farg->farg_type = btf_add_used_type_1 (ctfc, farg->farg_type,
    1204                 :             :                                                    false, false,
    1205                 :             :                                                    create_fixups);
    1206                 :             :             /* Note: argument names are stored in the auxilliary string table,
    1207                 :             :                since CTF does not include arg names.  That table has not been
    1208                 :             :                cleared, so no need to re-add argument names here.  */
    1209                 :             :           }
    1210                 :             :         break;
    1211                 :             :       }
    1212                 :             :     default:
    1213                 :             :       return NULL;
    1214                 :             :     }
    1215                 :             : 
    1216                 :             :   return dtd;
    1217                 :             : }
    1218                 :             : 
    1219                 :             : /* Recursively add type DTD and any types it references to the used set.
    1220                 :             :    Return a type that should be used for references to DTD - usually DTD itself,
    1221                 :             :    but may be NULL if DTD corresponds to a type which will not be emitted.  */
    1222                 :             : 
    1223                 :             : static ctf_dtdef_ref
    1224                 :          13 : btf_add_used_type (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
    1225                 :             : {
    1226                 :          13 :   return btf_add_used_type_1 (ctfc, dtd, false, false, true);
    1227                 :             : }
    1228                 :             : 
    1229                 :             : /* Initial entry point of BTF generation, called at early_finish () after
    1230                 :             :    CTF information has possibly been output.  Translate all CTF information
    1231                 :             :    to BTF, and do any processing that must be done early, such as creating
    1232                 :             :    BTF_KIND_FUNC records.  */
    1233                 :             : 
    1234                 :             : void
    1235                 :         110 : btf_early_finish (void)
    1236                 :             : {
    1237                 :         110 :   ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
    1238                 :             : 
    1239                 :         110 :   vec_alloc (funcs, 16);
    1240                 :         110 :   func_map = hash_map<ctf_dtdef_ref, ctf_dtdef_ref>::create_ggc (16);
    1241                 :             : 
    1242                 :             :   /* Note: from this point on, destructive changes are made to the TU CTFC to
    1243                 :             :      translate CTF to BTF.  If CTF debug info has also been requested, it must
    1244                 :             :      be emitted before starting the translation to BTF.  */
    1245                 :         110 :   btf_add_const_void (tu_ctfc);
    1246                 :         110 :   btf_add_func_records (tu_ctfc);
    1247                 :             : 
    1248                 :             :   /* These fields are reset to count BTF types etc.  */
    1249                 :         110 :   tu_ctfc->ctfc_num_types = 0;
    1250                 :         110 :   tu_ctfc->ctfc_num_vlen_bytes = 0;
    1251                 :         110 :   tu_ctfc->ctfc_vars_list_count = 0;
    1252                 :             : 
    1253                 :         110 :   if (debug_prune_btf)
    1254                 :             :     {
    1255                 :           5 :       btf_used_types
    1256                 :           5 :         = hash_set<ctf_dtdef_ref>::create_ggc (tu_ctfc->ctfc_types->elements ());
    1257                 :           5 :       tu_ctfc->ctfc_nextid = 1;
    1258                 :           5 :       fixups.create (1);
    1259                 :             : 
    1260                 :             :       /* Empty the string table, which was already populated with strings for
    1261                 :             :          all types translated from DWARF.  We may only need a very small subset
    1262                 :             :          of these strings; those will be re-added below.  */
    1263                 :           5 :       ctfc_delete_strtab (&tu_ctfc->ctfc_strtable);
    1264                 :           5 :       init_ctf_strtable (&tu_ctfc->ctfc_strtable);
    1265                 :           5 :       tu_ctfc->ctfc_strlen++;
    1266                 :             :     }
    1267                 :         110 : }
    1268                 :             : 
    1269                 :             : /* Push a BTF datasec entry ENTRY into the datasec named SECNAME,
    1270                 :             :    creating the datasec record if it does not already exist.  */
    1271                 :             : 
    1272                 :             : static void
    1273                 :         100 : btf_datasec_push_entry (ctf_container_ref ctfc, const char *secname,
    1274                 :             :                         struct btf_datasec_entry entry)
    1275                 :             : {
    1276                 :         100 :   if (secname == NULL)
    1277                 :          48 :     return;
    1278                 :             : 
    1279                 :             :   /* If we already have a datasec record for the appropriate section,
    1280                 :             :      append the new entry to it.  */
    1281                 :         124 :   for (size_t i = 0; i < datasecs.length (); i++)
    1282                 :          72 :     if (strcmp (datasecs[i].name, secname) == 0)
    1283                 :             :       {
    1284                 :          48 :         datasecs[i].entries.safe_push (entry);
    1285                 :          48 :         return;
    1286                 :             :       }
    1287                 :             : 
    1288                 :             :   /* If we don't already have a datasec record for secname, make one.  */
    1289                 :          52 :   uint32_t str_off;
    1290                 :          52 :   ctf_add_string (ctfc, secname, &str_off, CTF_AUX_STRTAB);
    1291                 :          52 :   if (strcmp (secname, ""))
    1292                 :          52 :     ctfc->ctfc_aux_strlen += strlen (secname) + 1;
    1293                 :             : 
    1294                 :             :   /* Note: ID will be assigned just before output.  */
    1295                 :          52 :   btf_datasec_t ds;
    1296                 :          52 :   ds.name = secname;
    1297                 :          52 :   ds.name_offset = str_off;
    1298                 :             : 
    1299                 :             :   /* Insert the entry into the new datasec record.  */
    1300                 :          52 :   ds.entries.create (1);
    1301                 :          52 :   ds.entries.quick_push (entry);
    1302                 :             : 
    1303                 :             :   /* Insert the datasec record itself.  */
    1304                 :          52 :   datasecs.safe_push (ds);
    1305                 :             : }
    1306                 :             : 
    1307                 :             : /* Create a datasec entry for a function, and insert it into the datasec
    1308                 :             :    record for the appropriate section.  Create the record if it does not
    1309                 :             :    yet exist.  */
    1310                 :             : 
    1311                 :             : static void
    1312                 :           4 : btf_datasec_add_func (ctf_container_ref ctfc, cgraph_node *func,
    1313                 :             :                       ctf_dtdef_ref func_dtd)
    1314                 :             : {
    1315                 :           4 :   const char *section_name = get_section_name (func);
    1316                 :             : 
    1317                 :             :   /* Note: get_section_name () returns NULL for functions in text
    1318                 :             :      section.  This is intentional, since we do not want to generate
    1319                 :             :      DATASEC entries for them.  */
    1320                 :           4 :   if (section_name == NULL)
    1321                 :           1 :     return;
    1322                 :             : 
    1323                 :           3 :   struct btf_datasec_entry entry;
    1324                 :           3 :   gcc_assert (func_dtd);
    1325                 :           3 :   entry.dtd = func_dtd;
    1326                 :           3 :   entry.is_var = false;
    1327                 :             : 
    1328                 :             :   /* Size is left as zero at compile time, to be filled in by loaders
    1329                 :             :      such as libbpf.  */
    1330                 :           3 :   entry.size = 0;
    1331                 :             : 
    1332                 :           3 :   btf_datasec_push_entry (ctfc, section_name, entry);
    1333                 :             : }
    1334                 :             : 
    1335                 :             : /* Create a datasec entry for a variable, and insert it into the datasec
    1336                 :             :    record for the appropriate section.  Create the record if it does not
    1337                 :             :    yet exist.  */
    1338                 :             : 
    1339                 :             : static void
    1340                 :         101 : btf_datasec_add_var (ctf_container_ref ctfc, varpool_node *var,
    1341                 :             :                      ctf_dvdef_ref dvd)
    1342                 :             : {
    1343                 :             :   /* PR112849: avoid assuming a section for extern decls without
    1344                 :             :      an explicit section, which would result in incorrectly
    1345                 :             :      emitting a BTF_KIND_DATASEC entry for them.  */
    1346                 :         101 :   if (DECL_EXTERNAL (var->decl) && var->get_section () == NULL)
    1347                 :           4 :     return;
    1348                 :             : 
    1349                 :          97 :   const char *section_name = get_section_name (var);
    1350                 :          97 :   if (section_name == NULL)
    1351                 :             :     return;
    1352                 :             : 
    1353                 :          97 :   gcc_assert (dvd);
    1354                 :          97 :   struct btf_datasec_entry entry;
    1355                 :          97 :   entry.dvd = dvd;
    1356                 :          97 :   entry.is_var = true;
    1357                 :          97 :   entry.size = 0;
    1358                 :             : 
    1359                 :          97 :   tree size = DECL_SIZE_UNIT (var->decl);
    1360                 :          97 :   if (tree_fits_uhwi_p (size))
    1361                 :          96 :     entry.size = tree_to_uhwi (size);
    1362                 :           1 :   else if (VOID_TYPE_P (TREE_TYPE (var->decl)))
    1363                 :           1 :     entry.size = 1;
    1364                 :             : 
    1365                 :          97 :   btf_datasec_push_entry (ctfc, section_name, entry);
    1366                 :             : }
    1367                 :             : 
    1368                 :             : /* Add datasec entries for functions to CTFC.  */
    1369                 :             : 
    1370                 :             : static void
    1371                 :         110 : btf_add_func_datasec_entries (ctf_container_ref ctfc)
    1372                 :             : {
    1373                 :             :   /* We need to create FUNC records at early_finish, so that we have them
    1374                 :             :      even for functions which are later inlined by optimization passes.
    1375                 :             :      But on the other hand, we do not want datasec entries for such functions,
    1376                 :             :      so only create the datasec entries for them late.  This loop will not
    1377                 :             :      hit functions which have already been inlined.  */
    1378                 :         110 :   cgraph_node *func;
    1379                 :         384 :   FOR_EACH_FUNCTION (func)
    1380                 :             :     {
    1381                 :          82 :       dw_die_ref die = lookup_decl_die (func->decl);
    1382                 :          82 :       if (die == NULL)
    1383                 :           0 :         continue;
    1384                 :             : 
    1385                 :          82 :       ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, die);
    1386                 :          82 :       if (dtd == NULL)
    1387                 :           0 :         continue;
    1388                 :             : 
    1389                 :          82 :       ctf_dtdef_ref *pdtd = func_map->get (dtd);
    1390                 :          82 :       if (pdtd && DECL_EXTERNAL (func->decl))
    1391                 :           4 :         btf_datasec_add_func (ctfc, func, *pdtd);
    1392                 :             :     }
    1393                 :         110 : }
    1394                 :             : 
    1395                 :             : /* Helper function used to determine whether or not a BTF_KIND_VAR record
    1396                 :             :    for the variable VAR shall be emitted.  */
    1397                 :             : 
    1398                 :             : static bool
    1399                 :         102 : btf_emit_variable_p (ctf_container_ref ctfc, varpool_node *var,
    1400                 :             :                      ctf_dvdef_ref *pdvd)
    1401                 :             : {
    1402                 :         102 :   dw_die_ref die = lookup_decl_die (var->decl);
    1403                 :         102 :   if (die == NULL)
    1404                 :             :     return false;
    1405                 :             : 
    1406                 :         102 :   ctf_dvdef_ref dvd = ctf_dvd_lookup (ctfc, die);
    1407                 :         102 :   if (dvd == NULL)
    1408                 :             :     return false;
    1409                 :             : 
    1410                 :             :   /* If this is an extern variable declaration with a defining declaration
    1411                 :             :      later, skip it so that only the defining declaration is emitted.
    1412                 :             :      This is the same case, fix and reasoning as in CTF; see PR105089.  */
    1413                 :         102 :   if (ctf_dvd_ignore_lookup (ctfc, dvd->dvd_key))
    1414                 :             :     return false;
    1415                 :             : 
    1416                 :             :   /* Skip variables with unrepresentable types.  */
    1417                 :         102 :   if (!btf_emit_type_p (dvd->dvd_type))
    1418                 :             :     return false;
    1419                 :             : 
    1420                 :         101 :   *pdvd = dvd;
    1421                 :         101 :   return true;
    1422                 :             : }
    1423                 :             : 
    1424                 :             : /* Add BTF_KIND_VAR records for variables.  */
    1425                 :             : 
    1426                 :             : static void
    1427                 :         110 : btf_add_vars (ctf_container_ref ctfc)
    1428                 :             : {
    1429                 :         110 :   size_t num_ctf_vars = ctfc->ctfc_vars->elements ();
    1430                 :             : 
    1431                 :         110 :   ctfc->ctfc_vars_list = ggc_vec_alloc<ctf_dvdef_ref>(num_ctf_vars);
    1432                 :             : 
    1433                 :         110 :   varpool_node *var;
    1434                 :         110 :   ctf_dvdef_ref dvd;
    1435                 :         322 :   FOR_EACH_VARIABLE (var)
    1436                 :             :     {
    1437                 :         102 :       if (!btf_emit_variable_p (ctfc, var, &dvd))
    1438                 :           1 :         continue;
    1439                 :             : 
    1440                 :             :       /* Mark 'extern' variables.  */
    1441                 :         101 :       if (DECL_EXTERNAL (var->decl))
    1442                 :           6 :         dvd->dvd_visibility = BTF_VAR_GLOBAL_EXTERN;
    1443                 :             : 
    1444                 :             :       /* Add the variable to the vars list.  */
    1445                 :         101 :       ctfc->ctfc_vars_list[ctfc->ctfc_vars_list_count++] = dvd;
    1446                 :             : 
    1447                 :             :       /* Add a BTF_KIND_DATASEC entry for the variable.  */
    1448                 :         101 :       btf_datasec_add_var (ctfc, var, dvd);
    1449                 :             : 
    1450                 :         107 :       const char *section = var->get_section ();
    1451                 :           5 :       if (section && (strcmp (section, ".maps") == 0) && debug_prune_btf)
    1452                 :             :         {
    1453                 :             :           /* The .maps section has special meaning in BTF: it is used for BPF
    1454                 :             :              map definitions.  These definitions should be structs.  We must
    1455                 :             :              collect pointee types used in map members as though they are used
    1456                 :             :              directly, effectively ignoring (from the pruning perspective) that
    1457                 :             :              they are struct members.  */
    1458                 :           1 :           ctf_dtdef_ref dtd = dvd->dvd_type;
    1459                 :           1 :           uint32_t kind = btf_dtd_kind (dvd->dvd_type);
    1460                 :           1 :           if (kind == BTF_KIND_STRUCT)
    1461                 :             :             {
    1462                 :           1 :               ctf_dmdef_t *dmd;
    1463                 :           1 :               for (dmd = dtd->dtd_u.dtu_members;
    1464                 :           3 :                    dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd))
    1465                 :           2 :                 btf_add_used_type (ctfc, dmd->dmd_type);
    1466                 :             :             }
    1467                 :             :         }
    1468                 :             :     }
    1469                 :         110 : }
    1470                 :             : 
    1471                 :             : /* Callback used by btf_assign_type_ids to insert types into their initial
    1472                 :             :    positions in the type list.  */
    1473                 :             : 
    1474                 :             : static int
    1475                 :         367 : btf_type_list_cb (ctf_dtdef_ref *slot, ctf_container_ref ctfc)
    1476                 :             : {
    1477                 :         367 :   ctf_dtdef_ref dtd = *slot;
    1478                 :         367 :   ctfc->ctfc_types_list[dtd->dtd_type] = dtd;
    1479                 :         367 :   return 1;
    1480                 :             : }
    1481                 :             : 
    1482                 :             : /* Construct the initial type list and assign BTF IDs for all types translated
    1483                 :             :    from CTF.  */
    1484                 :             : 
    1485                 :             : static void
    1486                 :         105 : btf_collect_translated_types (ctf_container_ref ctfc)
    1487                 :             : {
    1488                 :         105 :   size_t num_ctf_types = ctfc->ctfc_types->elements ();
    1489                 :             : 
    1490                 :             :   /* First, place each type at its CTF-assigned index in the list.
    1491                 :             :      The '+1' here and below is to account for the implicit void type with
    1492                 :             :      ID 0.  There is no real type at index 0 in the list.  */
    1493                 :         105 :   ctfc->ctfc_types_list = ggc_vec_alloc<ctf_dtdef_ref>(num_ctf_types + 1);
    1494                 :         367 :   ctfc->ctfc_types->traverse<ctf_container_ref, btf_type_list_cb> (ctfc);
    1495                 :             : 
    1496                 :             :   /* Now, pass through the list and adjust IDs to account for types which will
    1497                 :             :      not be emitted.  This results in each type that will be emitted in BTF
    1498                 :             :      being assigned an appropriate ID.  Note that types which will not be
    1499                 :             :      emitted remain in the list; they are skipped at output time.  */
    1500                 :         105 :   unsigned int skip = 0;
    1501                 :         472 :   for (size_t i = 1; i <= num_ctf_types; i++)
    1502                 :             :     {
    1503                 :         367 :       ctf_dtdef_ref dtd = ctfc->ctfc_types_list[i];
    1504                 :         367 :       if (!btf_emit_type_p (dtd))
    1505                 :             :         {
    1506                 :          19 :           dtd->dtd_type = BTF_INVALID_TYPEID;
    1507                 :          19 :           skip += 1;
    1508                 :          19 :           continue;
    1509                 :             :         }
    1510                 :             : 
    1511                 :         348 :       dtd->dtd_type -= skip;
    1512                 :         348 :       ctfc->ctfc_num_types++;
    1513                 :         348 :       ctfc->ctfc_num_vlen_bytes += btf_calc_num_vbytes (dtd);
    1514                 :             :     }
    1515                 :             : 
    1516                 :         105 :   max_translated_id = ctfc->ctfc_num_types;
    1517                 :         105 :   ctfc->ctfc_nextid = ctfc->ctfc_num_types + 1;
    1518                 :         105 : }
    1519                 :             : 
    1520                 :             : /* Assign BTF IDs for FUNC records and account for their size.  */
    1521                 :             : 
    1522                 :             : static void
    1523                 :         110 : btf_assign_func_ids (ctf_container_ref ctfc)
    1524                 :             : {
    1525                 :         110 :   ctf_dtdef_ref dtd;
    1526                 :         110 :   unsigned int i;
    1527                 :         193 :   FOR_EACH_VEC_ELT (*funcs, i, dtd)
    1528                 :             :     {
    1529                 :          83 :       dtd->dtd_type = ctfc->ctfc_nextid++;
    1530                 :          83 :       ctfc->ctfc_num_types++;
    1531                 :             :     }
    1532                 :         110 : }
    1533                 :             : 
    1534                 :             : /* Assign BTF IDs for variables and account for their size.  */
    1535                 :             : 
    1536                 :             : static void
    1537                 :         110 : btf_assign_var_ids (ctf_container_ref ctfc)
    1538                 :             : {
    1539                 :         211 :   for (size_t i = 0; i < ctfc->ctfc_vars_list_count; i++)
    1540                 :             :     {
    1541                 :         101 :       ctf_dvdef_ref dvd = ctfc->ctfc_vars_list[i];
    1542                 :         101 :       ctf_id_t id = ctfc->ctfc_nextid++;
    1543                 :         101 :       gcc_assert (id <= BTF_MAX_TYPE);
    1544                 :         101 :       dvd->dvd_id = id;
    1545                 :             : 
    1546                 :         101 :       ctfc->ctfc_num_types++;
    1547                 :         101 :       ctfc->ctfc_num_vlen_bytes += sizeof (struct btf_var);
    1548                 :             :     }
    1549                 :         110 : }
    1550                 :             : 
    1551                 :             : /* Assign BTF IDs for type and decl tags and account for their size.  */
    1552                 :             : 
    1553                 :             : static void
    1554                 :         105 : btf_assign_tag_ids (ctf_container_ref ctfc)
    1555                 :             : {
    1556                 :         105 :   size_t num_tags = vec_safe_length (ctfc->ctfc_tags);
    1557                 :         105 :   if (num_tags == 0)
    1558                 :         105 :     return;
    1559                 :             : 
    1560                 :             :   unsigned int i;
    1561                 :             :   ctf_dtdef_ref dtd;
    1562                 :          34 :   FOR_EACH_VEC_ELT (*ctfc->ctfc_tags, i, dtd)
    1563                 :             :     {
    1564                 :             :       /* Assign BTF id.  */
    1565                 :          26 :       ctf_id_t id = ctfc->ctfc_nextid++;
    1566                 :          26 :       gcc_assert (id <= BTF_MAX_TYPE);
    1567                 :          26 :       dtd->dtd_type = id;
    1568                 :             : 
    1569                 :             :       /* Tags on functions will have a ref_type pointing to the
    1570                 :             :          FUNC_PROTO, we want them to point the FUNC record instead.  */
    1571                 :          26 :       ctf_dtdef_ref *pdtd = NULL;
    1572                 :          26 :       if (dtd->ref_type && (pdtd = func_map->get (dtd->ref_type)) != NULL)
    1573                 :           5 :         dtd->ref_type = *pdtd;
    1574                 :             : 
    1575                 :             :       /* Strings for tags are stored in the auxiliary strtab, which is
    1576                 :             :          concatenated after the regular strtab.  ctti_name only accounts
    1577                 :             :          for offset in the auxiliary strtab until this point.  */
    1578                 :          26 :       dtd->dtd_data.ctti_name += ctfc_get_strtab_len (ctfc, CTF_STRTAB);
    1579                 :          26 :       ctfc->ctfc_num_types++;
    1580                 :          26 :       ctfc->ctfc_num_vlen_bytes += btf_calc_num_vbytes (dtd);
    1581                 :             :     }
    1582                 :             : }
    1583                 :             : 
    1584                 :             : /* Assign BTF IDs for datasec records and account for their size.  */
    1585                 :             : 
    1586                 :             : static void
    1587                 :         110 : btf_assign_datasec_ids (ctf_container_ref ctfc)
    1588                 :             : {
    1589                 :         162 :   for (size_t i = 0; i < datasecs.length (); i++)
    1590                 :             :     {
    1591                 :          52 :       datasecs[i].id = ctfc->ctfc_nextid++;
    1592                 :          52 :       datasecs[i].name_offset += ctfc_get_strtab_len (ctfc, CTF_STRTAB);
    1593                 :          52 :       ctfc->ctfc_num_types++;
    1594                 :          52 :       ctfc->ctfc_num_vlen_bytes += (datasecs[i].entries.length ()
    1595                 :          52 :                                     * sizeof (struct btf_var_secinfo));
    1596                 :             :     }
    1597                 :         110 : }
    1598                 :             : 
    1599                 :             : 
    1600                 :             : /* Manually mark that type T is used to ensure it will not be pruned.
    1601                 :             :    Used by the BPF backend when generating BPF CO-RE to mark types used
    1602                 :             :    in CO-RE relocations.  */
    1603                 :             : 
    1604                 :             : void
    1605                 :           0 : btf_mark_type_used (tree t)
    1606                 :             : {
    1607                 :             :   /* If we are not going to prune anyway, this is a no-op.  */
    1608                 :           0 :   if (!debug_prune_btf)
    1609                 :             :     return;
    1610                 :             : 
    1611                 :           0 :   gcc_assert (TYPE_P (t));
    1612                 :           0 :   ctf_container_ref ctfc = ctf_get_tu_ctfc ();
    1613                 :           0 :   ctf_dtdef_ref dtd = ctf_lookup_tree_type (ctfc, t);
    1614                 :             : 
    1615                 :           0 :   if (!dtd)
    1616                 :             :     return;
    1617                 :             : 
    1618                 :           0 :   btf_add_used_type (ctfc, dtd);
    1619                 :             : }
    1620                 :             : 
    1621                 :             : /* Callback used for assembling the only-used-types list.  Note that this is
    1622                 :             :    the same as btf_type_list_cb above, but the hash_set traverse requires a
    1623                 :             :    different function signature.  */
    1624                 :             : 
    1625                 :             : static bool
    1626                 :          50 : btf_used_type_list_cb (const ctf_dtdef_ref& dtd, ctf_container_ref ctfc)
    1627                 :             : {
    1628                 :          50 :   ctfc->ctfc_types_list[dtd->dtd_type] = dtd;
    1629                 :          50 :   return true;
    1630                 :             : }
    1631                 :             : 
    1632                 :             : /* Collect the set of types reachable from global variables and functions.
    1633                 :             :    This is the minimal set of types, used when generating pruned BTF.  */
    1634                 :             : 
    1635                 :             : static void
    1636                 :           5 : btf_collect_pruned_types (ctf_container_ref ctfc)
    1637                 :             : {
    1638                 :           5 :   vec_alloc (forwards, 1);
    1639                 :             : 
    1640                 :             :   /* Add types used from functions.  */
    1641                 :           5 :   ctf_dtdef_ref dtd;
    1642                 :           5 :   size_t i;
    1643                 :          13 :   FOR_EACH_VEC_ELT (*funcs, i, dtd)
    1644                 :             :     {
    1645                 :           3 :       btf_add_used_type (ctfc, dtd->ref_type);
    1646                 :           3 :       ctf_add_string (ctfc, dtd->dtd_name, &(dtd->dtd_data.ctti_name),
    1647                 :             :                       CTF_STRTAB);
    1648                 :             :     }
    1649                 :             : 
    1650                 :             :   /* Add types used from global variables.  */
    1651                 :           8 :   for (i = 0; i < ctfc->ctfc_vars_list_count; i++)
    1652                 :             :     {
    1653                 :           3 :       ctf_dvdef_ref dvd = ctfc->ctfc_vars_list[i];
    1654                 :           3 :       btf_add_used_type (ctfc, dvd->dvd_type);
    1655                 :           3 :       ctf_add_string (ctfc, dvd->dvd_name, &(dvd->dvd_name_offset), CTF_STRTAB);
    1656                 :             :     }
    1657                 :             : 
    1658                 :             :   /* Used type tags will be added by recursive btf_add_used_type calls above.
    1659                 :             :      For decl tags, scan the list and only add those decl tags whose referent
    1660                 :             :      types are marked as used.  We may have pruned a struct type with members
    1661                 :             :      annotated by a decl tag.  */
    1662                 :          11 :   FOR_EACH_VEC_ELT (*ctfc->ctfc_tags, i, dtd)
    1663                 :             :     {
    1664                 :             :       /* Only add decl tags whose referent types have not been pruned.
    1665                 :             :          Variables are never pruned, so decl tags on variables are always
    1666                 :             :          used.  */
    1667                 :           6 :       if (btf_dtd_kind (dtd) == BTF_KIND_DECL_TAG
    1668                 :           6 :           && ((dtd->ref_type && btf_used_types->contains (dtd->ref_type))
    1669                 :           2 :               || (dtd->dtd_u.dtu_tag.ref_var)))
    1670                 :           5 :         btf_add_used_type (ctfc, dtd);
    1671                 :             : 
    1672                 :             :       /* Tags on functions or function args will have a ref_type pointing to the
    1673                 :             :          FUNC_PROTO, we want them to point the FUNC record instead.  */
    1674                 :           6 :       ctf_dtdef_ref *pdtd = NULL;
    1675                 :           6 :       if (dtd->ref_type
    1676                 :           5 :           && btf_used_types->contains (dtd->ref_type)
    1677                 :          10 :           && (pdtd = func_map->get (dtd->ref_type)) != NULL)
    1678                 :           2 :         dtd->ref_type = *pdtd;
    1679                 :             :     }
    1680                 :             : 
    1681                 :             :   /* Process fixups.  If the base type was never added, create a forward for it
    1682                 :             :      and adjust the reference to point to that.  If it was added, then nothing
    1683                 :             :      needs to change.  */
    1684                 :           7 :   for (i = 0; i < fixups.length (); i++)
    1685                 :             :     {
    1686                 :           2 :       struct btf_fixup *fx = &fixups[i];
    1687                 :           2 :       if (!btf_used_types->contains (fx->pointee_dtd))
    1688                 :             :         {
    1689                 :             :           /* The underlying type is not used.  Create a forward.  */
    1690                 :           2 :           ctf_dtdef_ref fwd = ggc_cleared_alloc<ctf_dtdef_t> ();
    1691                 :           2 :           ctf_id_t id = ctfc->ctfc_nextid++;
    1692                 :           2 :           gcc_assert (id <= BTF_MAX_TYPE);
    1693                 :             : 
    1694                 :           2 :           bool union_p = (btf_dtd_kind (fx->pointee_dtd) == BTF_KIND_UNION);
    1695                 :             : 
    1696                 :           2 :           fwd->dtd_name = fx->pointee_dtd->dtd_name;
    1697                 :           2 :           fwd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_FORWARD, union_p, 0);
    1698                 :           2 :           fwd->dtd_type = id;
    1699                 :           2 :           ctfc->ctfc_num_types++;
    1700                 :           2 :           ctfc->ctfc_num_vlen_bytes += btf_calc_num_vbytes (fwd);
    1701                 :           2 :           ctf_add_string (ctfc, fwd->dtd_name, &(fwd->dtd_data.ctti_name),
    1702                 :             :                           CTF_STRTAB);
    1703                 :             : 
    1704                 :             :           /* Update the pointer to point to the forward.  */
    1705                 :           2 :           fx->pointer_dtd->ref_type = fwd;
    1706                 :           2 :           vec_safe_push (forwards, fwd);
    1707                 :             :         }
    1708                 :             :     }
    1709                 :             : 
    1710                 :             :   /* Construct the resulting pruned type list.  */
    1711                 :           5 :   ctfc->ctfc_types_list
    1712                 :           5 :     = ggc_vec_alloc<ctf_dtdef_ref> (btf_used_types->elements () + 1
    1713                 :          10 :                                     + vec_safe_length (forwards));
    1714                 :             : 
    1715                 :          50 :   btf_used_types->traverse<ctf_container_ref, btf_used_type_list_cb> (ctfc);
    1716                 :             : 
    1717                 :             :   /* Insert the newly created forwards into the regular types list too.  */
    1718                 :          12 :   FOR_EACH_VEC_ELT (*forwards, i, dtd)
    1719                 :           2 :     ctfc->ctfc_types_list[dtd->dtd_type] = dtd;
    1720                 :             : 
    1721                 :           5 :   max_translated_id = btf_used_types->elements () + vec_safe_length (forwards);
    1722                 :           5 : }
    1723                 :             : 
    1724                 :             : /* Late entry point for BTF generation, called from dwarf2out_finish ().
    1725                 :             :    Complete and emit BTF information.  */
    1726                 :             : 
    1727                 :             : void
    1728                 :         110 : btf_finish (void)
    1729                 :             : {
    1730                 :         110 :   ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
    1731                 :         110 :   init_btf_sections ();
    1732                 :             : 
    1733                 :         110 :   datasecs.create (0);
    1734                 :             : 
    1735                 :         110 :   btf_add_vars (tu_ctfc);
    1736                 :         110 :   if (debug_prune_btf)
    1737                 :             :     {
    1738                 :             :       /* Collect pruned set of BTF types and prepare for emission.
    1739                 :             :          This includes only types directly used in file-scope variables and
    1740                 :             :          function return/argument types.  */
    1741                 :           5 :       btf_collect_pruned_types (tu_ctfc);
    1742                 :             :     }
    1743                 :             :   else
    1744                 :             :     {
    1745                 :             :       /* Collect all BTF types and prepare for emission.
    1746                 :             :          This includes all types translated from DWARF.  */
    1747                 :         105 :       btf_collect_translated_types (tu_ctfc);
    1748                 :             :     }
    1749                 :         110 :   btf_add_func_datasec_entries (tu_ctfc);
    1750                 :             : 
    1751                 :         110 :   btf_assign_var_ids (tu_ctfc);
    1752                 :         110 :   btf_assign_func_ids (tu_ctfc);
    1753                 :             : 
    1754                 :             :   /* Both decl and type tags may be pruned if the types/decls to which they
    1755                 :             :      refer are pruned.  This is handled in btf_collect_pruned_types, and
    1756                 :             :      through that process they have also been assigned ids already.  */
    1757                 :         110 :   if (!debug_prune_btf)
    1758                 :         105 :     btf_assign_tag_ids (tu_ctfc);
    1759                 :             : 
    1760                 :         110 :   btf_assign_datasec_ids (tu_ctfc);
    1761                 :             : 
    1762                 :             :   /* Finally, write out the complete .BTF section.  */
    1763                 :         110 :   btf_output (tu_ctfc);
    1764                 :             : 
    1765                 :             :   /* If compiling for BPF with CO-RE info, we cannot deallocate until after the
    1766                 :             :      contents of the .BTF.ext section are finalized, which happens very late in
    1767                 :             :      BPF backend.  Therefore, the deallocation (i.e. btf_finalize ()) is delayed
    1768                 :             :      until TARGET_ASM_FILE_END for BPF CO-RE.  */
    1769                 :         110 :   if (!btf_with_core_debuginfo_p ())
    1770                 :         110 :     btf_finalize ();
    1771                 :         110 : }
    1772                 :             : 
    1773                 :             : /* Reset all state for BTF generation so that we can rerun the compiler within
    1774                 :             :    the same process.  */
    1775                 :             : 
    1776                 :             : void
    1777                 :         110 : btf_finalize (void)
    1778                 :             : {
    1779                 :         110 :   btf_info_section = NULL;
    1780                 :         110 :   max_translated_id = 0;
    1781                 :             : 
    1782                 :         162 :   for (size_t i = 0; i < datasecs.length (); i++)
    1783                 :          52 :     datasecs[i].entries.release ();
    1784                 :         110 :   datasecs.release ();
    1785                 :             : 
    1786                 :         110 :   funcs = NULL;
    1787                 :         110 :   if (func_map)
    1788                 :             :     {
    1789                 :         110 :       func_map->empty ();
    1790                 :         110 :       func_map = NULL;
    1791                 :             :     }
    1792                 :             : 
    1793                 :         110 :   if (debug_prune_btf)
    1794                 :             :     {
    1795                 :           5 :       if (btf_used_types)
    1796                 :             :         {
    1797                 :           5 :           btf_used_types->empty ();
    1798                 :           5 :           btf_used_types = NULL;
    1799                 :             :         }
    1800                 :             : 
    1801                 :           5 :       fixups.release ();
    1802                 :           5 :       forwards = NULL;
    1803                 :             :     }
    1804                 :             : 
    1805                 :         110 :   ctf_container_ref tu_ctfc = ctf_get_tu_ctfc ();
    1806                 :         110 :   ctfc_delete_container (tu_ctfc);
    1807                 :         110 :   tu_ctfc = NULL;
    1808                 :         110 : }
    1809                 :             : 
    1810                 :             : /* Traversal function for all BTF_KIND_FUNC type records.  */
    1811                 :             : 
    1812                 :             : bool
    1813                 :           0 : traverse_btf_func_types (funcs_traverse_callback callback, void *data)
    1814                 :             : {
    1815                 :           0 :   ctf_dtdef_ref ref;
    1816                 :           0 :   unsigned i;
    1817                 :           0 :   FOR_EACH_VEC_ELT (*funcs, i, ref)
    1818                 :             :     {
    1819                 :           0 :       bool stop = callback (ref, data);
    1820                 :           0 :       if (stop == true)
    1821                 :             :         return true;
    1822                 :             :     }
    1823                 :             :   return false;
    1824                 :             : }
    1825                 :             : 
    1826                 :             : #include "gt-btfout.h"
        

Generated by: LCOV version 2.1-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.