[PATCH][WIP] libiberty: Support for relocation output
This patch teaches libiberty to output X86-64 Relocations. >From d3b2d168369e76a6fac2b3b3cbd591ccf22ea8ea Mon Sep 17 00:00:00 2001 From: Rishi Raj Date: Mon, 23 Oct 2023 06:22:44 +0530 Subject: [PATCH 1/3] Extended libiberty to output X86_64 relocations Signed-off-by: Rishi Raj --- include/simple-object.h | 11 +- libiberty/simple-object-common.h | 28 ++- libiberty/simple-object-elf.c| 317 ++- libiberty/simple-object.c| 95 +++-- 4 files changed, 387 insertions(+), 64 deletions(-) diff --git a/include/simple-object.h b/include/simple-object.h index 3a14184b12c..17ecd856636 100644 --- a/include/simple-object.h +++ b/include/simple-object.h @@ -186,6 +186,14 @@ simple_object_write_add_data (simple_object_write *simple_object, simple_object_write_section *section, const void *buffer, size_t size, int copy, int *err); +/* Add relocation to SECTION in SIMPLE_OBJECT */ +void +simple_object_write_add_relocation (simple_object_write_section *section, + unsigned long offset, long addend, const char *name, unsigned long rel_sec_idx); + +/* Modifies simple object section buffer at offset. */ +void simple_object_modify_buffer (simple_object_write_section *section, + unsigned long offset, unsigned char *buffer, int copy); /* Write the complete object file to DESCRIPTOR, an open file descriptor. This returns NULL on success. On error this returns @@ -199,7 +207,8 @@ simple_object_write_to_file (simple_object_write *simple_object, object_write_to_file function*/ extern void simple_object_write_add_symbol(simple_object_write *sobj, const char *name, -size_t size, unsigned int align); + unsigned int value, size_t size, unsigned char bind, + unsigned char type, unsigned short int shndx, unsigned char st_other); /* Release all resources associated with SIMPLE_OBJECT, including any simple_object_write_section's that may have been created. */ diff --git a/libiberty/simple-object-common.h b/libiberty/simple-object-common.h index df99c9d85ac..1dc06908eec 100644 --- a/libiberty/simple-object-common.h +++ b/libiberty/simple-object-common.h @@ -73,9 +73,17 @@ struct simple_object_symbol_struct /*The name of this symbol. */ char *name; /* Symbol value */ - unsigned int align; + unsigned int value; /* Symbol size */ size_t size; + /*Symbol binding*/ + unsigned char bind; + /*Symbol info*/ + unsigned char type; + /*Symbol section index*/ + unsigned short int shndx; + /* Symbol visibility */ + unsigned char st_other; }; /* A section in an object file being created. */ @@ -93,6 +101,11 @@ struct simple_object_write_section_struct struct simple_object_write_section_buffer *buffers; /* The last data attached to this section. */ struct simple_object_write_section_buffer *last_buffer; + /*The first relocation attached to this section. */ + struct simple_object_write_section_relocation *relocations; + /* The last relocation attache to this section. */ + struct simple_object_write_section_relocation *last_relocation; + }; /* Data attached to a section. */ @@ -108,6 +121,19 @@ struct simple_object_write_section_buffer /* A buffer to free, or NULL. */ void *free_buffer; }; +struct simple_object_write_section_relocation +{ + /* The next relocation for this section. */ + struct simple_object_write_section_relocation *next; + /* The offset. */ + unsigned long offset; + /* Addend */ + long addend; + /* Relocation symbol */ + const char *name; + /* Relocation symbol st_shndx wrt .debug_info index */ + unsigned long rel_sec_idx; +}; /* The number of bytes we read from the start of the file to pass to the match function. */ diff --git a/libiberty/simple-object-elf.c b/libiberty/simple-object-elf.c index 86b7a27dc74..0bbaf4b489f 100644 --- a/libiberty/simple-object-elf.c +++ b/libiberty/simple-object-elf.c @@ -238,6 +238,7 @@ typedef struct #define STT_NOTYPE 0 /* Symbol type is unspecified */ #define STT_OBJECT 1 /* Symbol is a data object */ #define STT_FUNC 2 /* Symbol is a code object */ +#define STT_SECTION 3 /* Symbol is associate with a section */ #define STT_TLS 6 /* Thread local data object */ #define STT_GNU_IFUNC 10 /* Symbol is an indirect code object */ @@ -248,6 +249,63 @@ typedef struct #define STV_DEFAULT 0 /* Visibility is specified by binding type */ #define STV_HIDDEN 2 /* Can only be seen inside currect component */ +typedef struct +{ + unsigned char r_offset[4]; /* Address */ + unsigned char r_info[4]; /* relocation type and symbol index */ +} Elf32_External_Rel; + +typedef struct +{ + unsigned char r_offset[8]; /* Address */ + unsigned char r_info[8]; /* Relocation type and symbol index */ +} Elf64_External_Rel; +typedef struct +{ + unsigned char r_offset[4]; /* Address */ + unsigned char r_info[4]; /* Relocation type and symbol index */ + char r_addend[4]; /
Re: [PATCH][WIP] libiberty: Support for relocation output
On Mon, 23 Oct 2023 at 16:30, Jan Hubicka wrote: > > This patch teaches libiberty to output X86-64 Relocations. > Hello, > for actual patch submission you will need to add changelog :) > I know, right :). > > diff --git a/libiberty/simple-object-elf.c > b/libiberty/simple-object-elf.c > > index 86b7a27dc74..0bbaf4b489f 100644 > > --- a/libiberty/simple-object-elf.c > > +++ b/libiberty/simple-object-elf.c > > @@ -238,6 +238,7 @@ typedef struct > > #define STT_NOTYPE 0 /* Symbol type is unspecified */ > > #define STT_OBJECT 1 /* Symbol is a data object */ > > #define STT_FUNC 2 /* Symbol is a code object */ > > +#define STT_SECTION 3 /* Symbol is associate with a section */ > Associated I guess. > > #define STT_TLS 6 /* Thread local data object */ > > #define STT_GNU_IFUNC 10 /* Symbol is an indirect code object */ > > > > @@ -248,6 +249,63 @@ typedef struct > > #define STV_DEFAULT 0 /* Visibility is specified by binding type */ > > #define STV_HIDDEN 2 /* Can only be seen inside currect component */ > > > > +typedef struct > > +{ > > + unsigned char r_offset[4]; /* Address */ > > + unsigned char r_info[4]; /* relocation type and symbol index */ > > +} Elf32_External_Rel; > > + > > +typedef struct > > +{ > > + unsigned char r_offset[8]; /* Address */ > > + unsigned char r_info[8]; /* Relocation type and symbol index */ > > +} Elf64_External_Rel; > > +typedef struct > > +{ > > + unsigned char r_offset[4]; /* Address */ > > + unsigned char r_info[4]; /* Relocation type and symbol index */ > > + char r_addend[4]; /* Addend */ > > +} Elf32_External_Rela; > > +typedef struct > > +{ > > + unsigned char r_offset[8]; /* Address */ > > + unsigned char r_info[8]; /* Relocation type and symbol index */ > > + unsigned char r_addend[8]; /* Addend */ > > +} Elf64_External_Rela; > > + > > +/* How to extract and insert information held in the r_info field. */ > > + > > +#define ELF32_R_SYM(val) ((val) >> 8) > > +#define ELF32_R_TYPE(val) ((val) & 0xff) > > +#define ELF32_R_INFO(sym, type) (((sym) << 8) + ((type) & 0xff)) > > + > > +#define ELF64_R_SYM(i) ((i) >> 32) > > +#define ELF64_R_TYPE(i) ((i) & 0x) > > +#define ELF64_R_INFO(sym,type) unsigned long) (sym)) << 32) + > (type)) > > + > > +/* AMD x86-64 relocations. */ > > +#define R_X86_64_NONE 0 /* No reloc */ > > +#define R_X86_64_64 1 /* Direct 64 bit */ > > +#define R_X86_64_PC32 2 /* PC relative 32 bit signed */ > > +#define R_X86_64_GOT32 3 /* 32 bit GOT entry */ > > +#define R_X86_64_PLT32 4 /* 32 bit PLT address */ > > +#define R_X86_64_COPY 5 /* Copy symbol at runtime */ > > +#define R_X86_64_GLOB_DAT 6 /* Create GOT entry */ > > +#define R_X86_64_JUMP_SLOT 7 /* Create PLT entry */ > > +#define R_X86_64_RELATIVE 8 /* Adjust by program base */ > > +#define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative > > + offset to GOT */ > > +#define R_X86_64_32 10 /* Direct 32 bit zero extended */ > > +#define R_X86_64_32S 11 /* Direct 32 bit sign extended */ > > +#define R_X86_64_16 12 /* Direct 16 bit zero extended */ > > This will eventually need to go into per-architecture table. > You support only those needed for Dwarf2out ouptut, right? > Yeah, as of now. > > I think we need Iant's opinion on thi part of patch (he is the > maintainer of simple-object) but to me it looks reasonable. For longer > term it will be necessary to think how to make this extensible to other > architectures without writting too much of code. (have some more > declarative way to specify relocations we output) > Make sense. > > Honza >
Re: [PATCH][WIP] dwarf2out: extend to output debug section directly to object file during debug_early phase
On Mon, 23 Oct 2023 at 18:18, Jan Hubicka wrote: > Hello, > thanks for the patch. > > Overall it looks in right direction except for the code duplication in > output_die and friends. > > +/* Given a die and id, produce the appropriate abbreviations > > + directly to lto object file */ > > + > > +static void > > +output_die_abbrevs_to_object_file(unsigned long abbrev_id, dw_die_ref > > abbrev) > > +{ > > + unsigned ix; > > + dw_attr_node *a_attr; > > + > > + output_data_uleb128_to_object_file(abbrev_id); > > + output_data_uleb128_to_object_file(abbrev->die_tag); > > + > > + > > + if (abbrev->die_child != NULL) > > +output_data_to_object_file(1,DW_children_yes); > > + else > > +output_data_to_object_file(1,DW_children_no); > > + > > + for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++) > > +{ > > + output_data_uleb128_to_object_file(a_attr->dw_attr); > > + output_value_format_to_object_file(a_attr); > > + if (value_format (a_attr) == DW_FORM_implicit_const) > > + { > > + if (AT_class (a_attr) == dw_val_class_file_implicit) > > +{ > > + int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file); > > + output_data_sleb128_to_object_file(f); > > +} > > + else > > + output_data_sleb128_to_object_file(a_attr->dw_attr_val.v.val_int); > > + } > > +} > > + > > + output_data_to_object_file (1, 0); > > + output_data_to_object_file (1, 0); > > So this basically renames dw2_asm_output_data to > output_data_to_object_file and similarly for other output functions. > > What would be main problems of making dw2_asm_* functions to do the > right thing when outputting to object file? > Either by conditionals or turning them to virtual functions/hooks as > Richi suggested? > I think it's doable via conditionals. Can you explain the second approach in more detail? > > It may be performance critical how quickly we sput out the bytecode. > In future we may templateize this, but right now it is likely premature > optimization. > Cool. > > > > +struct lto_simple_object > lto_simple_object is declared in lto frontend. Why do you need to > duplicate it here? > > It looks like adding relocations should be abstracted by lto API, > so you don't need to look inside this structure that is > lto/lto-object.cc only. > I should have taken this approach, but instead, I exposed simple objects to dwarf2out. That's the reason to duplicate the above struct. I will take care of this while refactoring and abstracting it by lto API > > > +/* Output one line number table into the .debug_line section. */ > > + > > +static void > > +output_one_line_info_table (dw_line_info_table *table) > It is hard to tell from the diff. Did you just moved these functions > earlier in source file? > Yeah. I will refactor the dwarf2out soon to clear these confusions. -- Rishi > > Honza >
[COMMITTED] MAINTAINERS file: Added myself to Write After Approval and DCO
From 50cb9df7209125f9466336d23efdd4fbeda9c4d5 Mon Sep 17 00:00:00 2001 From: rsh-raj Date: Fri, 30 Jun 2023 16:04:48 +0530 Subject: [PATCH] MAINTAINERS file: Added myself to Write After Approval and DCO ChangeLog: 2023-06-30 Rishi Raj * MAINTAINERS: Added myself to Write After Approval and DCO --- MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index bac773ad0af..2a0eb5b52b5 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -610,6 +610,7 @@ Hafiz Abid Qadeer Yao Qi Jerry Quinn Navid Rahimi +Rishi Raj Easwaran Raman Joe Ramsay Rolf Rasmussen @@ -749,6 +750,7 @@ Immad Mir Gaius Mulley Siddhesh Poyarekar Navid Rahimi +Rishi Raj Trevor Saunders Bill Schmidt Nathan Sidwell -- 2.40.1
[PATCH] lto: Bypass assembler when generating LTO object files. & libiberty: lto: Addition of .symtab in elf file.
This series of two patches enables the output of the LTO object file without an assembler. As of now, .symtab is emitted with __gnu_lto_slim symbol. To test, follow the instructions in the commit message of patch 1. Also, as suggested by Honza, I am putting these patches on devel/bypass-asm branch. >From 46c766d242fd248abc49201cf6419735c8415a6f Mon Sep 17 00:00:00 2001 From: Rishi Raj Date: Sat, 1 Jul 2023 10:28:11 +0530 Subject: [PATCH 1/2] lto: Bypass assembler when generating LTO object files. This patch applies Jan Hubicka previous patch on current sources. Now the compiler is able to produce object files without assembler, although a lot of things are missing, such as __lto_slim symbol, debug symbols etc. They will be added in future patches. To test this current patch, use these commands below. 1) ./xgcc -B ./ -O3 a.c -flto -S -fbypass-asm=crtbegin.o -o a.o 2) ./xgcc -B ./ -O2 a.o -flto 3) ./a.out We are currently working with elf-only support (mach-o, coff, xcoff etc will be dealt later) so this will only work on a linux machine. I have tested this on my machine ( Arch linux, Machine: Advanced Micro Devices X86-64) and all LTO test cases passed as expected. gcc/ChangeLog: * Makefile.in: * common.opt: * langhooks.cc (lhd_begin_section): (lhd_append_data): (lhd_end_section): * lto/lto-object.cc: Moved to... * lto-object.cc: ...here. * lto-streamer.h (struct lto_section_slot): (struct lto_section_list): (struct lto_file): (lto_obj_file_open): (lto_obj_file_close): (lto_obj_build_section_table): (lto_obj_create_section_hash_table): (lto_obj_begin_section): (lto_obj_append_data): (lto_obj_end_section): (lto_set_current_out_file): (lto_get_current_out_file): * toplev.cc (compile_file): (lang_dependent_init): gcc/lto/ChangeLog: * Make-lang.in: * lto-common.cc (lto_file_read): * lto-lang.cc: * lto.h (struct lto_file): (lto_obj_file_open): (lto_obj_file_close): (struct lto_section_list): (lto_obj_build_section_table): (lto_obj_create_section_hash_table): (lto_obj_begin_section): (lto_obj_append_data): (lto_obj_end_section): (lto_set_current_out_file): (lto_get_current_out_file): (struct lto_section_slot): Signed-off-by: Rishi Raj --- gcc/Makefile.in | 1 + gcc/common.opt | 3 +++ gcc/langhooks.cc| 29 +++- gcc/{lto => }/lto-object.cc | 29 +--- gcc/lto-streamer.h | 35 ++ gcc/lto/Make-lang.in| 4 ++-- gcc/lto/lto-common.cc | 3 ++- gcc/lto/lto-lang.cc | 1 + gcc/lto/lto.h | 38 - gcc/toplev.cc | 19 --- 10 files changed, 110 insertions(+), 52 deletions(-) rename gcc/{lto => }/lto-object.cc (94%) diff --git a/gcc/Makefile.in b/gcc/Makefile.in index c478ec85201..c9ae222fb59 100644 --- a/gcc/Makefile.in +++ b/gcc/Makefile.in @@ -1560,6 +1560,7 @@ OBJS = \ lto-section-out.o \ lto-opts.o \ lto-compress.o \ + lto-object.o \ mcf.o \ mode-switching.o \ modulo-sched.o \ diff --git a/gcc/common.opt b/gcc/common.opt index 25f650e2dae..ba7a18ece8c 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -1169,6 +1169,9 @@ fbtr-bb-exclusive Common Ignore Does nothing. Preserved for backward compatibility. +fbypass-asm= +Common Joined Var(flag_bypass_asm) + fcallgraph-info Common RejectNegative Var(flag_callgraph_info) Init(NO_CALLGRAPH_INFO); Output callgraph information on a per-file basis. diff --git a/gcc/langhooks.cc b/gcc/langhooks.cc index 9a1a9eccca9..a76ed974d58 100644 --- a/gcc/langhooks.cc +++ b/gcc/langhooks.cc @@ -38,6 +38,10 @@ along with GCC; see the file COPYING3. If not see #include "stor-layout.h" #include "cgraph.h" #include "debug.h" +#include "function.h" +#include "basic-block.h" +#include "gimple.h" +#include "lto-streamer.h" /* Do nothing; in many cases the default hook. */ @@ -817,6 +821,19 @@ lhd_begin_section (const char *name) { section *section; + if (flag_bypass_asm) +{ + static int initialized = false; + if (!initialized) + { + gcc_assert (asm_out_file == NULL); + lto_set_current_out_file (lto_obj_file_open (asm_file_name, true)); + initialized = true; + } + lto_obj_begin_section (name); + return; +} + /* Save the old section so we can restore it in lto_end_asm_section. */ gcc_assert (!saved_section); saved
[PATCH] lto: bypass-asm: Fixed test(U*) used but never defined error.
>From 5151cf943987347edbc3707f08f0da8cd9f49f88 Mon Sep 17 00:00:00 2001 From: Rishi Raj Date: Fri, 7 Jul 2023 10:15:57 +0530 Subject: [PATCH] lto: Fixed test(U*) used but never defined error. This Patch fixes the error during bootstrapped build. Signed-off-by: Rishi Raj --- gcc/lto-object.cc | 1 + gcc/lto/lto-lang.cc | 1 + 2 files changed, 2 insertions(+) diff --git a/gcc/lto-object.cc b/gcc/lto-object.cc index 33eca5a7d81..097c81a686e 100644 --- a/gcc/lto-object.cc +++ b/gcc/lto-object.cc @@ -31,6 +31,7 @@ along with GCC; see the file COPYING3. If not see #include "gimple.h" #include "diagnostic-core.h" #include "tm.h" +#include "cgraph.h" #include "lto-streamer.h" #include "lto-section-names.h" #include "simple-object.h" diff --git a/gcc/lto/lto-lang.cc b/gcc/lto/lto-lang.cc index cf33bf178c2..35f60325c80 100644 --- a/gcc/lto/lto-lang.cc +++ b/gcc/lto/lto-lang.cc @@ -26,6 +26,7 @@ along with GCC; see the file COPYING3. If not see #include "basic-block.h" #include "tree.h" #include "gimple.h" +#include "cgraph.h" #include "stringpool.h" #include "diagnostic-core.h" #include "stor-layout.h" -- 2.40.1