aboutsummaryrefslogtreecommitdiff
path: root/bfd/bfd.c
diff options
context:
space:
mode:
Diffstat (limited to 'bfd/bfd.c')
-rw-r--r--bfd/bfd.c153
1 files changed, 150 insertions, 3 deletions
diff --git a/bfd/bfd.c b/bfd/bfd.c
index 5ae5eca..ba78cf3 100644
--- a/bfd/bfd.c
+++ b/bfd/bfd.c
@@ -85,7 +85,7 @@ CODE_FRAGMENT
. ENUM_BITFIELD (bfd_direction) direction : 2;
.
. {* Format_specific flags. *}
-. flagword flags : 17;
+. flagword flags : 18;
.
. {* Values that may appear in the flags field of a BFD. These also
. appear in the object_flags field of the bfd_target structure, where
@@ -162,14 +162,19 @@ CODE_FRAGMENT
. {* BFD is a dummy, for plugins. *}
.#define BFD_PLUGIN 0x10000
.
+. {* Compress sections in this BFD with SHF_COMPRESSED from gABI. *}
+.#define BFD_COMPRESS_GABI 0x20000
+.
. {* Flags bits to be saved in bfd_preserve_save. *}
.#define BFD_FLAGS_SAVED \
-. (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_PLUGIN)
+. (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_PLUGIN \
+. | BFD_COMPRESS_GABI)
.
. {* Flags bits which are for BFD use only. *}
.#define BFD_FLAGS_FOR_BFD_USE_MASK \
. (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \
-. | BFD_PLUGIN | BFD_TRADITIONAL_FORMAT | BFD_DETERMINISTIC_OUTPUT)
+. | BFD_PLUGIN | BFD_TRADITIONAL_FORMAT | BFD_DETERMINISTIC_OUTPUT \
+. | BFD_COMPRESS_GABI)
.
. {* Is the file descriptor being cached? That is, can it be closed as
. needed, and re-opened when accessed later? *}
@@ -1940,3 +1945,145 @@ bfd_demangle (bfd *abfd, const char *name, int options)
return res;
}
+
+/*
+FUNCTION
+ bfd_update_compression_header
+
+SYNOPSIS
+ void bfd_update_compression_header
+ (bfd *abfd, bfd_byte *contents, asection *sec);
+
+DESCRIPTION
+ Set the compression header at CONTENTS of SEC in ABFD and update
+ elf_section_flags for compression.
+*/
+
+void
+bfd_update_compression_header (bfd *abfd, bfd_byte *contents,
+ asection *sec)
+{
+ if ((abfd->flags & BFD_COMPRESS) != 0)
+ {
+ if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
+ {
+ if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
+ {
+ const struct elf_backend_data *bed
+ = get_elf_backend_data (abfd);
+
+ /* Set the SHF_COMPRESSED bit. */
+ elf_section_flags (sec) |= SHF_COMPRESSED;
+
+ if (bed->s->elfclass == ELFCLASS32)
+ {
+ Elf32_External_Chdr *echdr
+ = (Elf32_External_Chdr *) contents;
+ bfd_put_32 (abfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
+ bfd_put_32 (abfd, sec->size, &echdr->ch_size);
+ bfd_put_32 (abfd, 1 << sec->alignment_power,
+ &echdr->ch_addralign);
+ }
+ else
+ {
+ Elf64_External_Chdr *echdr
+ = (Elf64_External_Chdr *) contents;
+ bfd_put_64 (abfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
+ bfd_put_64 (abfd, sec->size, &echdr->ch_size);
+ bfd_put_64 (abfd, 1 << sec->alignment_power,
+ &echdr->ch_addralign);
+ }
+ }
+ else
+ /* Clear the SHF_COMPRESSED bit. */
+ elf_section_flags (sec) &= ~SHF_COMPRESSED;
+ }
+ }
+ else
+ abort ();
+}
+
+/*
+ FUNCTION
+ bfd_check_compression_header
+
+ SYNOPSIS
+ bfd_boolean bfd_check_compression_header
+ (bfd *abfd, bfd_byte *contents, asection *sec,
+ bfd_size_type uncompressed_size);
+
+DESCRIPTION
+ Check the compression header at CONTENTS of SEC in ABFD with
+ the uncompressed size UNCOMPRESSED_SIZE.
+
+RETURNS
+ Return TRUE if the compression header is valid.
+*/
+
+bfd_boolean
+bfd_check_compression_header (bfd *abfd, bfd_byte *contents,
+ asection *sec,
+ bfd_size_type uncompressed_size)
+{
+ if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
+ && (elf_section_flags (sec) & SHF_COMPRESSED) != 0)
+ {
+ Elf_Internal_Chdr chdr;
+ const struct elf_backend_data *bed = get_elf_backend_data (abfd);
+ if (bed->s->elfclass == ELFCLASS32)
+ {
+ Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
+ chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
+ chdr.ch_size = bfd_get_32 (abfd, &echdr->ch_size);
+ chdr.ch_addralign = bfd_get_32 (abfd, &echdr->ch_addralign);
+ }
+ else
+ {
+ Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
+ chdr.ch_type = bfd_get_64 (abfd, &echdr->ch_type);
+ chdr.ch_size = bfd_get_64 (abfd, &echdr->ch_size);
+ chdr.ch_addralign = bfd_get_64 (abfd, &echdr->ch_addralign);
+ }
+ return (chdr.ch_type == ELFCOMPRESS_ZLIB
+ && chdr.ch_size == uncompressed_size
+ && chdr.ch_addralign == 1U << sec->alignment_power);
+ }
+
+ return FALSE;
+}
+
+/*
+FUNCTION
+ bfd_get_compression_header_size
+
+SYNOPSIS
+ int bfd_get_compression_header_size (bfd *abfd, asection *sec);
+
+DESCRIPTION
+ Return the size of the compression header of SEC in ABFD.
+
+RETURNS
+ Return the size of the compression header in bytes.
+*/
+
+int
+bfd_get_compression_header_size (bfd *abfd, asection *sec)
+{
+ if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
+ {
+ if (sec == NULL)
+ {
+ if (!(abfd->flags & BFD_COMPRESS_GABI))
+ return 0;
+ }
+ else if (!(elf_section_flags (sec) & SHF_COMPRESSED))
+ return 0;
+
+ if (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS32)
+ return sizeof (Elf32_External_Chdr);
+ else
+ return sizeof (Elf64_External_Chdr);
+ }
+
+ return 0;
+}