Changeset da13982 in mainline for kernel/meson.build


Ignore:
Timestamp:
2023-10-26T15:20:07Z (15 months ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master, topic/msim-upgrade, topic/simplify-dev-export
Children:
2fbb42f
Parents:
d28bdbe
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2023-10-26 14:42:03)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2023-10-26 15:20:07)
Message:

Read symbol table from ELF sections

Instead of the currently broken data generated using genmap.py,
read the ELF symbol table for stack traces and symbol names.
In addition to that, prepare ground for accessing DWARF debug
sections.

Because neither .symtab, nor .debug_* sections are
normally part of the program image, these have to be provided
externally. Instead of the previous way of relinking kernel
to bake in the symbol data, we now only link kernel once
and the extra debug data is loaded as part of the initrd image.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/meson.build

    rd28bdbe rda13982  
    4141# Defines test_src
    4242subdir('test')
    43 
    44 ## Cross-platform assembly to start a symtab.data section
    45 #
    46 symtab_section = '.section symtab.data, "a", ' + atsign + 'progbits;'
    4743
    4844kernel_include_dirs = include_directories(
     
    9793endif
    9894
    99 if CONFIG_STRIP_BINARIES
    100         # TODO: do this after disassembling
    101         kernel_link_args += [ '-s' ]
    102 endif
    103 
    10495kernel_c_args = arch_kernel_c_args + kernel_defs + [
    10596        '-ffreestanding',
     
    149140all_kernel_objects = [ instrumentables, noninstrumentables ]
    150141
    151 # We iterate the build several times to get symbol table right.
    152 # Three times is sufficient to get correct even symbols after symtab.
     142kernel_name = 'kernel.elf'
     143kernel_map_name = kernel_name + '.map'
     144kernel_map_path = meson.current_build_dir() / kernel_map_name
     145
     146kernel_elf = executable(kernel_name,
     147        include_directories: kernel_include_dirs,
     148        implicit_include_directories: false,
     149        c_args: kernel_c_args,
     150        link_args: kernel_c_args + kernel_link_args + [
     151                '-Wl,-Map,' + kernel_map_path,
     152        ],
     153        link_depends: kernel_ldscript,
     154        link_whole: all_kernel_objects,
     155        pie: false,
     156)
     157
     158kernel_dbg = custom_target('kernel.dbg',
     159        output: 'kernel.dbg',
     160        input: kernel_elf,
     161        command: [
     162                objcopy,
     163                '--only-keep-debug',
     164                '@INPUT@',
     165                '@OUTPUT@',
     166        ],
     167)
     168
     169kernel_elf_stripped = custom_target(kernel_name + '.stripped',
     170        output: kernel_name + '.stripped',
     171        input: kernel_elf,
     172        command: [
     173                objcopy,
     174                '--strip-unneeded',
     175                '@INPUT@',
     176                '@OUTPUT@',
     177        ],
     178)
     179
     180rd_init_binaries += [[ kernel_elf_stripped, 'boot/kernel.elf' ]]
     181install_files += [[ 'boot', kernel_elf_stripped.full_path(), 'kernel.elf' ]]
     182install_deps += [ kernel_elf_stripped ]
    153183
    154184if CONFIG_SYMTAB
    155         # Iterate build three times.
    156         iterations = [ 1, 2, 3 ]
    157 
    158         # Generates symbol table information as an object file.
    159         genmap = find_program('tools/genmap.py')
    160 
    161         # Symbol table dump needed for genmap.
    162         kernel_syms = custom_target('kernel_syms.txt',
    163                 input: all_kernel_objects,
    164                 output: 'kernel_syms.txt',
    165                 command: [ objdump, '-t', '@INPUT@' ],
    166                 capture: true,
    167         )
    168 else
    169         # Build just once.
    170         iterations = [ 1 ]
    171 endif
    172 
    173 # Empty symbol map for first iteration.
    174 kernel_map_S = custom_target('empty_map.S',
    175         output: 'empty_map.S',
    176         capture: true,
    177         command: [ 'echo', kernel_as_prolog + symtab_section ],
    178 )
    179 
    180 foreach iter : iterations
    181         is_last = (iter == iterations.length())
    182         kernel_name = 'kernel.@0@.elf'.format(iter)
    183         kernel_map_name = kernel_name + '.map'
    184         kernel_map_path = meson.current_build_dir() / kernel_map_name
    185 
    186         kernel_elf = executable(kernel_name, kernel_map_S,
    187                 include_directories: kernel_include_dirs,
    188                 implicit_include_directories: false,
    189                 c_args: kernel_c_args,
    190                 link_args: kernel_c_args + kernel_link_args + [
    191                         '-Wl,-Map,' + kernel_map_path,
    192                 ],
    193                 link_depends: kernel_ldscript,
    194                 link_whole: all_kernel_objects,
    195                 pie: false,
    196         )
    197 
    198         # Generate symbol table if this is not the final iteration.
    199         if not is_last
    200 
    201                 # TODO: Teach kernel to read its own ELF symbol table and get rid of this nonsense.
    202                 # Need to first make sure all architectures (even future ones with dumb bootloaders) can use ELF formatted kernel.
    203 
    204                 kernel_map_bin = custom_target(kernel_map_name + '.bin',
    205                         output: kernel_map_name + '.bin',
    206                         input: [ kernel_elf, kernel_syms ],
    207                         command: [ genmap, kernel_map_path, '@INPUT1@', '@OUTPUT@' ],
    208                 )
    209 
    210                 kernel_map_S_name = kernel_name + '.map.S'
    211 
    212                 kernel_map_S = custom_target(kernel_map_S_name,
    213                         input: kernel_map_bin,
    214                         output: kernel_map_S_name,
    215                         capture: true,
    216                         command: [ 'echo', kernel_as_prolog + symtab_section + ' .incbin "@INPUT@"' ],
    217                 )
    218         endif
    219 endforeach
    220 
    221 rd_init_binaries += [[ kernel_elf, 'boot/kernel.elf' ]]
    222 
    223 install_files += [[ 'boot', kernel_elf.full_path(), 'kernel.elf' ]]
    224 install_deps += [ kernel_elf ]
     185        rd_init_binaries += [[ kernel_dbg, 'kernel.dbg' ]]
     186        install_files += [[ 'boot', kernel_dbg.full_path(), 'kernel.dbg' ]]
     187        install_deps += [ kernel_dbg ]
     188endif
    225189
    226190kernel_disasm = custom_target('kernel.elf.disasm',
Note: See TracChangeset for help on using the changeset viewer.