Compare commits
7 Commits
v1.0.0-rc1
...
26eb480343
Author | SHA1 | Date | |
---|---|---|---|
26eb480343 | |||
30e47d533a | |||
faeab33375 | |||
7e414f8576 | |||
933680c80d | |||
891df1803e | |||
7be1d6a967 |
@@ -1,14 +1,14 @@
|
|||||||
# Maintainer: Mario Hüttel <mario (dot) huettel (!) gmx (dot) net>
|
# Maintainer: Mario Hüttel <mario (dot) huettel (!) gmx (dot) net>
|
||||||
|
|
||||||
pkgname=patchelfcrc
|
pkgname=patchelfcrc
|
||||||
pkgver=5e7f697
|
pkgver=v1.0.0_rc1
|
||||||
pkgrel=1
|
pkgrel=1
|
||||||
pkgdesc="Tool for patching CRC checksums of sections into ELF binaries"
|
pkgdesc="Tool for patching CRC checksums of sections into ELF binaries"
|
||||||
arch=('i686' 'x86_64')
|
arch=('i686' 'x86_64')
|
||||||
url="https://git.shimatta.de/mhu/patchelfcrc"
|
url="https://git.shimatta.de/mhu/patchelfcrc"
|
||||||
licence=('GPLv2')
|
licence=('GPLv2')
|
||||||
depends=('libelf' 'libxml2')
|
depends=('libelf' 'libxml2')
|
||||||
makedepends=('cmake' 'pandoc' 'git' 'gvim')
|
makedepends=('cmake' 'pandoc' 'git' 'gvim' 'bash')
|
||||||
provides=('patchelfcrc')
|
provides=('patchelfcrc')
|
||||||
source=("${pkgname}-git"::"git+https://git.shimatta.de/mhu/patchelfcrc" "git+https://git.shimatta.de/3rd-party/libfort.git" "git+https://git.shimatta.de/mhu/linklist-lib")
|
source=("${pkgname}-git"::"git+https://git.shimatta.de/mhu/patchelfcrc" "git+https://git.shimatta.de/3rd-party/libfort.git" "git+https://git.shimatta.de/mhu/linklist-lib")
|
||||||
sha1sums=('SKIP' 'SKIP' 'SKIP')
|
sha1sums=('SKIP' 'SKIP' 'SKIP')
|
||||||
|
@@ -21,14 +21,25 @@
|
|||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#define print_err(fmt, ...) fprintf(stderr, "[ERR] " fmt, ## __VA_ARGS__)
|
/**
|
||||||
|
* @brief Setting for reporting to console.
|
||||||
|
*/
|
||||||
|
enum reporting_color_mode {
|
||||||
|
COLOR_MODE_DETECT, /**< @brief Automatically detect if tty. If tty, color is used */
|
||||||
|
COLOR_MODE_COLOR, /**< @brief Force color mode on stderr */
|
||||||
|
COLOR_MODE_COLOR_OFF, /**< @brief Force no color on stderr */
|
||||||
|
};
|
||||||
|
|
||||||
#define print_warn(fmt, ...) fprintf(stderr, "[WARN] " fmt, ## __VA_ARGS__)
|
void print_err(const char *fmt, ...);
|
||||||
|
|
||||||
|
void print_warn(const char *fmt, ...);
|
||||||
|
|
||||||
void print_debug(const char *fmt, ...);
|
void print_debug(const char *fmt, ...);
|
||||||
|
|
||||||
void reporting_enable_verbose(void);
|
void reporting_enable_verbose(bool state);
|
||||||
|
|
||||||
bool reporting_get_verbosity(void);
|
bool reporting_get_verbosity(void);
|
||||||
|
|
||||||
|
void reporting_init(enum reporting_color_mode mode);
|
||||||
|
|
||||||
#endif /* _REPORTING_H_ */
|
#endif /* _REPORTING_H_ */
|
||||||
|
@@ -10,10 +10,10 @@ add_custom_command(
|
|||||||
OUTPUT
|
OUTPUT
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/${MAN_PAGE_NAME}
|
${CMAKE_CURRENT_BINARY_DIR}/${MAN_PAGE_NAME}
|
||||||
COMMAND
|
COMMAND
|
||||||
bash -c "pandoc \"${CMAKE_CURRENT_SOURCE_DIR}/patchelfcrc.1.md\" -s -t man | gzip > \"${CMAKE_CURRENT_BINARY_DIR}/${MAN_PAGE_NAME}\""
|
bash -c "cat \"${CMAKE_CURRENT_SOURCE_DIR}/patchelfcrc.1.md\" | sed \"s/!version!/`git describe --tags --always --dirty`/\" | pandoc -s -t man | gzip > \"${CMAKE_CURRENT_BINARY_DIR}/${MAN_PAGE_NAME}\""
|
||||||
VERBATIM
|
VERBATIM
|
||||||
WORKING_DIRECTORY
|
WORKING_DIRECTORY
|
||||||
${CMAKE_CURRENT_BINARY_DIR}
|
${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
MAIN_DEPENDENCY
|
MAIN_DEPENDENCY
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/patchelfcrc.1.md
|
${CMAKE_CURRENT_SOURCE_DIR}/patchelfcrc.1.md
|
||||||
)
|
)
|
@@ -1,4 +1,4 @@
|
|||||||
% patchelfcrc(1) 0.0.2
|
% patchelfcrc(1) !version!
|
||||||
% Mario Huettel
|
% Mario Huettel
|
||||||
% October 2022
|
% October 2022
|
||||||
|
|
||||||
@@ -84,6 +84,9 @@
|
|||||||
**--usage**
|
**--usage**
|
||||||
: Print usage hints on command line options.
|
: Print usage hints on command line options.
|
||||||
|
|
||||||
|
**--no-color**
|
||||||
|
: Force output on stdout and stderr to be pure text without color codes.
|
||||||
|
|
||||||
# EXAMPLES
|
# EXAMPLES
|
||||||
|
|
||||||
**patchelfcrc** --list-crcs
|
**patchelfcrc** --list-crcs
|
||||||
|
@@ -30,6 +30,19 @@
|
|||||||
#include <fort.h>
|
#include <fort.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <patchelfcrc/crc-output-struct.h>
|
#include <patchelfcrc/crc-output-struct.h>
|
||||||
|
#include <byteswap.h>
|
||||||
|
|
||||||
|
static const union {
|
||||||
|
uint8_t data[4];
|
||||||
|
uint32_t val;
|
||||||
|
} _endianess_check_union = {{1u, 2u, 3u, 4u}};
|
||||||
|
|
||||||
|
enum endianess {
|
||||||
|
END_LITTLE = 0x04030201ul,
|
||||||
|
END_BIG = 0x01020304ul,
|
||||||
|
};
|
||||||
|
|
||||||
|
#define HOST_ENDIANESS (_endianess_check_union.val)
|
||||||
|
|
||||||
struct elf_section {
|
struct elf_section {
|
||||||
GElf_Shdr section_header;
|
GElf_Shdr section_header;
|
||||||
@@ -77,15 +90,16 @@ static uint32_t get_uint32_from_byte_string(const uint8_t *data, bool little_end
|
|||||||
uint32_t out = 0ul;
|
uint32_t out = 0ul;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
/* Always shift in in big endian format */
|
||||||
for (i = 0; i < 4; i++) {
|
for (i = 0; i < 4; i++) {
|
||||||
if (little_endian)
|
|
||||||
out >>= 8u;
|
|
||||||
else
|
|
||||||
out <<= 8u;
|
out <<= 8u;
|
||||||
|
out |= (uint32_t)data[i];
|
||||||
out |= (((uint32_t)data[i]) << (little_endian ? 24u : 0u));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Swap bytes if little endian */
|
||||||
|
if (little_endian)
|
||||||
|
out = bswap_32(out);
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,14 +110,12 @@ static void write_crc_to_byte_array(uint8_t *byte_array, uint32_t crc, uint8_t c
|
|||||||
if (!byte_array)
|
if (!byte_array)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (!little_endian)
|
||||||
|
crc = bswap_32(crc);
|
||||||
|
|
||||||
for (i = 0; i < crc_size_bytes; i++) {
|
for (i = 0; i < crc_size_bytes; i++) {
|
||||||
if (little_endian) {
|
byte_array[i] = (uint8_t)(crc & 0xFFul);
|
||||||
byte_array[i] = (uint8_t)(crc & 0xFFul);
|
crc >>= 8u;
|
||||||
crc >>= 8u;
|
|
||||||
} else {
|
|
||||||
byte_array[i] = (uint8_t)((crc & 0xFF000000ul) >> 24u);
|
|
||||||
crc <<= 8u;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,6 +148,14 @@ static const char *section_type_to_str(Elf64_Word type)
|
|||||||
return "INIT_ARRAY";
|
return "INIT_ARRAY";
|
||||||
case SHT_FINI_ARRAY:
|
case SHT_FINI_ARRAY:
|
||||||
return "FINI_ARRAY";
|
return "FINI_ARRAY";
|
||||||
|
case SHT_PREINIT_ARRAY:
|
||||||
|
return "PREINIT_ARRAY";
|
||||||
|
case SHT_DYNAMIC:
|
||||||
|
return "DYNAMIC";
|
||||||
|
case SHT_ARM_ATTRIBUTES:
|
||||||
|
return "ARM_ATTRIBUTES";
|
||||||
|
case SHT_ARM_PREEMPTMAP:
|
||||||
|
return "ARM_PREEMPTMAP";
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -147,6 +167,7 @@ static void print_sections(elfpatch_handle_t *ep)
|
|||||||
SlList *iter;
|
SlList *iter;
|
||||||
ft_table_t *table;
|
ft_table_t *table;
|
||||||
const struct elf_section *section;
|
const struct elf_section *section;
|
||||||
|
bool alloc, write, exec;
|
||||||
|
|
||||||
ret_if_ep_err(ep);
|
ret_if_ep_err(ep);
|
||||||
|
|
||||||
@@ -162,15 +183,23 @@ static void print_sections(elfpatch_handle_t *ep)
|
|||||||
|
|
||||||
/* Write header */
|
/* Write header */
|
||||||
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
|
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
|
||||||
ft_write_ln(table, "Section", "Type", "Size", "VMA", "LMA", "File Offset");
|
ft_write_ln(table, "Section", "Type", "ALLOC", "WRITE", "EXEC", "Size", "VMA", "LMA", "File Offset");
|
||||||
|
|
||||||
for (iter = ep->sections; iter; iter = sl_list_next(iter)) {
|
for (iter = ep->sections; iter; iter = sl_list_next(iter)) {
|
||||||
section = (const struct elf_section *)iter->data;
|
section = (const struct elf_section *)iter->data;
|
||||||
if (!section)
|
if (!section)
|
||||||
continue;
|
continue;
|
||||||
ft_printf_ln(table, "%s|%s|%lu|%p|%p|%p",
|
|
||||||
|
alloc = !!(section->section_header.sh_flags & SHF_ALLOC);
|
||||||
|
write = !!(section->section_header.sh_flags & SHF_WRITE);
|
||||||
|
exec = !!(section->section_header.sh_flags & SHF_EXECINSTR);
|
||||||
|
|
||||||
|
ft_printf_ln(table, "%s|%s|%s|%s|%s|%lu|%p|%p|%p",
|
||||||
section->name,
|
section->name,
|
||||||
section_type_to_str(section->section_header.sh_type),
|
section_type_to_str(section->section_header.sh_type),
|
||||||
|
alloc ? "x" : "",
|
||||||
|
write ? "x" : "",
|
||||||
|
exec ? "x" : "",
|
||||||
section->section_header.sh_size,
|
section->section_header.sh_size,
|
||||||
(void *)section->section_header.sh_addr,
|
(void *)section->section_header.sh_addr,
|
||||||
(void *)section->lma,
|
(void *)section->lma,
|
||||||
@@ -261,6 +290,12 @@ static int elf_patch_read_program_headers(elfpatch_handle_t *ep)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (header_count == 0) {
|
||||||
|
/* No program headers found. This ELF file is probably not linked */
|
||||||
|
ep->program_headers_count = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
ep->program_headers = (GElf_Phdr *)malloc(header_count * sizeof(GElf_Phdr));
|
ep->program_headers = (GElf_Phdr *)malloc(header_count * sizeof(GElf_Phdr));
|
||||||
if (!ep->program_headers) {
|
if (!ep->program_headers) {
|
||||||
/* Mem error. Abort. Program will crash eventually */
|
/* Mem error. Abort. Program will crash eventually */
|
||||||
@@ -306,9 +341,11 @@ static void resolve_section_lmas(elfpatch_handle_t *ep)
|
|||||||
if (!sec)
|
if (!sec)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
/* By default each sections LMA is assumed to be its LMA as well */
|
||||||
|
sec->lma = (uint64_t)sec->section_header.sh_addr;
|
||||||
|
|
||||||
if (sec->section_header.sh_type == SHT_NOBITS) {
|
if (sec->section_header.sh_type == SHT_NOBITS) {
|
||||||
/* Section does not contain data. It may be allocated but is not loaded. Therefore, LMA=VMA. */
|
/* Section does not contain data. It may be allocated but is not loaded. Therefore, LMA=VMA. */
|
||||||
sec->lma = (uint64_t)sec->section_header.sh_addr;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -433,12 +470,12 @@ elfpatch_handle_t *elf_patch_open(const char *path, bool readonly, bool expect_l
|
|||||||
case 1:
|
case 1:
|
||||||
print_debug("ELF Endianess: little\n");
|
print_debug("ELF Endianess: little\n");
|
||||||
if (!expect_little_endian)
|
if (!expect_little_endian)
|
||||||
print_err("Big endian format expected. File is little endian. Double check settings!\n");
|
print_warn("Big endian format expected. File is little endian. Double check settings!\n");
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
print_debug("ELF Endianess: big\n");
|
print_debug("ELF Endianess: big\n");
|
||||||
if (expect_little_endian)
|
if (expect_little_endian)
|
||||||
print_err("Little endian format expected. File is big endian. Double check settings!\n");
|
print_warn("Little endian format expected. File is big endian. Double check settings!\n");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
print_err("Cannot determine endianess of ELF file. EI_DATA is: %d\n", ident[5]);
|
print_err("Cannot determine endianess of ELF file. EI_DATA is: %d\n", ident[5]);
|
||||||
@@ -651,6 +688,7 @@ int elf_patch_write_crcs_to_section(elfpatch_handle_t *ep, const char *output_se
|
|||||||
struct crc_out_struct_32bit crc_32bit;
|
struct crc_out_struct_32bit crc_32bit;
|
||||||
struct crc_out_struct_64bit crc_64bit;
|
struct crc_out_struct_64bit crc_64bit;
|
||||||
uint64_t in_sec_addr, in_sec_len;
|
uint64_t in_sec_addr, in_sec_len;
|
||||||
|
bool needs_byteswap;
|
||||||
|
|
||||||
ret_val_if_ep_err(ep, -1000);
|
ret_val_if_ep_err(ep, -1000);
|
||||||
|
|
||||||
@@ -748,6 +786,12 @@ int elf_patch_write_crcs_to_section(elfpatch_handle_t *ep, const char *output_se
|
|||||||
if (check_start_magic && crc_data->elf_bits == 64)
|
if (check_start_magic && crc_data->elf_bits == 64)
|
||||||
sec_bytes += 4u;
|
sec_bytes += 4u;
|
||||||
|
|
||||||
|
needs_byteswap = false;
|
||||||
|
if ((HOST_ENDIANESS != END_LITTLE && little_endian) ||
|
||||||
|
(HOST_ENDIANESS == END_LITTLE && !little_endian)) {
|
||||||
|
needs_byteswap = true;
|
||||||
|
}
|
||||||
|
|
||||||
for (iter = crc_data->crc_entries, idx = 0; iter; iter = sl_list_next(iter), idx++) {
|
for (iter = crc_data->crc_entries, idx = 0; iter; iter = sl_list_next(iter), idx++) {
|
||||||
crc_entry = (struct crc_entry *)iter->data;
|
crc_entry = (struct crc_entry *)iter->data;
|
||||||
in_sec_addr = use_vma ? crc_entry->vma : crc_entry->lma;
|
in_sec_addr = use_vma ? crc_entry->vma : crc_entry->lma;
|
||||||
@@ -758,18 +802,19 @@ int elf_patch_write_crcs_to_section(elfpatch_handle_t *ep, const char *output_se
|
|||||||
print_debug("Corresponding input section at 0x%"PRIx64", length: %"PRIu64"\n",
|
print_debug("Corresponding input section at 0x%"PRIx64", length: %"PRIu64"\n",
|
||||||
in_sec_addr,
|
in_sec_addr,
|
||||||
in_sec_len);
|
in_sec_len);
|
||||||
|
|
||||||
if (crc_data->elf_bits == 32) {
|
if (crc_data->elf_bits == 32) {
|
||||||
crc_32bit.crc = crc_entry->crc;
|
crc_32bit.crc = needs_byteswap ? bswap_32(crc_entry->crc) : crc_entry->crc;
|
||||||
crc_32bit.length = (uint32_t)in_sec_len;
|
crc_32bit.length = needs_byteswap ? bswap_32((uint32_t)in_sec_len) : (uint32_t)in_sec_len;
|
||||||
crc_32bit.start_address = (uint32_t)in_sec_addr;
|
crc_32bit.start_address = needs_byteswap ? bswap_32((uint32_t)in_sec_addr) : (uint32_t)in_sec_addr;
|
||||||
memcpy(sec_bytes, &crc_32bit, sizeof(crc_32bit));
|
memcpy(sec_bytes, &crc_32bit, sizeof(crc_32bit));
|
||||||
sec_bytes += sizeof(crc_32bit);
|
sec_bytes += sizeof(crc_32bit);
|
||||||
} else {
|
} else {
|
||||||
/* 64 bit case */
|
/* 64 bit case */
|
||||||
crc_64bit.crc = crc_entry->crc;
|
crc_64bit.crc = needs_byteswap ? bswap_32(crc_entry->crc) : crc_entry->crc;
|
||||||
crc_64bit._unused_dummy = 0ul;
|
crc_64bit._unused_dummy = 0ul;
|
||||||
crc_64bit.length = in_sec_len;
|
crc_64bit.length = needs_byteswap ? bswap_64(in_sec_len) : in_sec_len;
|
||||||
crc_64bit.start_address = in_sec_addr;
|
crc_64bit.start_address = needs_byteswap ? bswap_64(in_sec_addr) : in_sec_addr;
|
||||||
memcpy(sec_bytes, &crc_64bit, sizeof(crc_64bit));
|
memcpy(sec_bytes, &crc_64bit, sizeof(crc_64bit));
|
||||||
sec_bytes += sizeof(crc_64bit);
|
sec_bytes += sizeof(crc_64bit);
|
||||||
}
|
}
|
||||||
|
13
src/main.c
13
src/main.c
@@ -40,8 +40,10 @@ const char *argp_program_bug_address = "<mario [dot] huettel [at] linux [dot] co
|
|||||||
#define ARG_KEY_IMPORT (6)
|
#define ARG_KEY_IMPORT (6)
|
||||||
#define ARG_KEY_USE_VMA (7)
|
#define ARG_KEY_USE_VMA (7)
|
||||||
#define ARG_KEY_XSD (8)
|
#define ARG_KEY_XSD (8)
|
||||||
|
#define ARG_KEY_FORCE_NO_COLOR (9)
|
||||||
|
|
||||||
struct command_line_options {
|
struct command_line_options {
|
||||||
|
bool force_nocolor;
|
||||||
bool little_endian;
|
bool little_endian;
|
||||||
bool dry_run;
|
bool dry_run;
|
||||||
bool verbose;
|
bool verbose;
|
||||||
@@ -104,6 +106,9 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
|
|||||||
case ARG_KEY_USE_VMA:
|
case ARG_KEY_USE_VMA:
|
||||||
args->use_vma = true;
|
args->use_vma = true;
|
||||||
break;
|
break;
|
||||||
|
case ARG_KEY_FORCE_NO_COLOR:
|
||||||
|
args->force_nocolor = true;
|
||||||
|
break;
|
||||||
case 'p':
|
case 'p':
|
||||||
/* Polyniomial */
|
/* Polyniomial */
|
||||||
args->crc.polynomial = strtoull(arg, &endptr, 0);
|
args->crc.polynomial = strtoull(arg, &endptr, 0);
|
||||||
@@ -195,6 +200,7 @@ static int parse_cmdline_options(int *argc, char ***argv, struct command_line_op
|
|||||||
{"import", ARG_KEY_IMPORT, "XML", 0, "Do not caclulate CRCs but import them from file", 3},
|
{"import", ARG_KEY_IMPORT, "XML", 0, "Do not caclulate CRCs but import them from file", 3},
|
||||||
{"xsd", ARG_KEY_XSD, 0, 0, "Print XSD to stdout", 0},
|
{"xsd", ARG_KEY_XSD, 0, 0, "Print XSD to stdout", 0},
|
||||||
{"use-vma", ARG_KEY_USE_VMA, 0, 0, "Use the VMA instead of the LMA for struct output", 2},
|
{"use-vma", ARG_KEY_USE_VMA, 0, 0, "Use the VMA instead of the LMA for struct output", 2},
|
||||||
|
{"no-color", ARG_KEY_FORCE_NO_COLOR, 0, 0, "Force the output to be text only without colors", 0},
|
||||||
/* Sentinel */
|
/* Sentinel */
|
||||||
{NULL, 0, 0, 0, NULL, 0}
|
{NULL, 0, 0, 0, NULL, 0}
|
||||||
};
|
};
|
||||||
@@ -425,8 +431,11 @@ int main(int argc, char **argv)
|
|||||||
goto free_cmds;
|
goto free_cmds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Initialize console output */
|
||||||
|
reporting_init(cmd_opts.force_nocolor ? COLOR_MODE_COLOR_OFF : COLOR_MODE_DETECT);
|
||||||
|
|
||||||
if (cmd_opts.verbose || cmd_opts.dry_run)
|
if (cmd_opts.verbose || cmd_opts.dry_run)
|
||||||
reporting_enable_verbose();
|
reporting_enable_verbose(true);
|
||||||
|
|
||||||
print_verbose_start_info(&cmd_opts);
|
print_verbose_start_info(&cmd_opts);
|
||||||
|
|
||||||
@@ -450,7 +459,7 @@ int main(int argc, char **argv)
|
|||||||
print_warn("--use-vma option only has an effect when exporting as struct output.\n");
|
print_warn("--use-vma option only has an effect when exporting as struct output.\n");
|
||||||
|
|
||||||
if (!cmd_opts.output_section && cmd_opts.export_xml == NULL)
|
if (!cmd_opts.output_section && cmd_opts.export_xml == NULL)
|
||||||
print_err("No output section / XML export specified. Will continue but not create any output\n");
|
print_warn("No output section / XML export specified. Will continue but not create any output\n");
|
||||||
|
|
||||||
/* Prepare libelf for use with the latest ELF version */
|
/* Prepare libelf for use with the latest ELF version */
|
||||||
elf_version(EV_CURRENT);
|
elf_version(EV_CURRENT);
|
||||||
|
101
src/reporting.c
101
src/reporting.c
@@ -19,9 +19,66 @@
|
|||||||
#include <patchelfcrc/reporting.h>
|
#include <patchelfcrc/reporting.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
static bool global_verbosity_state = false;
|
static bool global_verbosity_state = false;
|
||||||
|
static bool reporting_use_color = false;
|
||||||
|
|
||||||
|
#define COLOR_RESET "\e[0m"
|
||||||
|
|
||||||
|
#define COLOR_BOLD_RED "\e[31;1m"
|
||||||
|
#define COLOR_RED "\e[31m"
|
||||||
|
|
||||||
|
#define COLOR_BOLD_YELLOW "\e[33;1m"
|
||||||
|
#define COLOR_YELLOW "\e[33m"
|
||||||
|
|
||||||
|
|
||||||
|
void print_err(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list va;
|
||||||
|
va_start(va, fmt);
|
||||||
|
|
||||||
|
/* Set color */
|
||||||
|
if (reporting_use_color)
|
||||||
|
fprintf(stderr, COLOR_BOLD_RED "[ERR]" COLOR_RESET " " COLOR_RED);
|
||||||
|
else
|
||||||
|
fprintf(stderr, "[ERR] ");
|
||||||
|
|
||||||
|
|
||||||
|
vfprintf(stderr, fmt, va);
|
||||||
|
|
||||||
|
/* Reset color */
|
||||||
|
if (reporting_use_color) {
|
||||||
|
fprintf(stderr, COLOR_RESET);
|
||||||
|
}
|
||||||
|
|
||||||
|
va_end(va);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_warn(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list va;
|
||||||
|
va_start(va, fmt);
|
||||||
|
|
||||||
|
/* Set color */
|
||||||
|
if (reporting_use_color)
|
||||||
|
fprintf(stderr, COLOR_BOLD_YELLOW "[WARN]" COLOR_RESET " " COLOR_YELLOW);
|
||||||
|
else
|
||||||
|
fprintf(stderr, "[WARN] ");
|
||||||
|
|
||||||
|
|
||||||
|
vfprintf(stderr, fmt, va);
|
||||||
|
|
||||||
|
/* Reset color */
|
||||||
|
if (reporting_use_color) {
|
||||||
|
fprintf(stderr, COLOR_RESET);
|
||||||
|
}
|
||||||
|
|
||||||
|
va_end(va);
|
||||||
|
}
|
||||||
|
|
||||||
void print_debug(const char *fmt, ...)
|
void print_debug(const char *fmt, ...)
|
||||||
{
|
{
|
||||||
@@ -34,12 +91,52 @@ void print_debug(const char *fmt, ...)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void reporting_enable_verbose(void)
|
void reporting_enable_verbose(bool state)
|
||||||
{
|
{
|
||||||
global_verbosity_state = true;
|
global_verbosity_state = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool reporting_get_verbosity(void)
|
bool reporting_get_verbosity(void)
|
||||||
{
|
{
|
||||||
return global_verbosity_state;
|
return global_verbosity_state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Check whether stderr supports colors.
|
||||||
|
* @note This function checks for a tty and the TERM environment variable. It has to contain "xterm".
|
||||||
|
* @return true if colors are supported
|
||||||
|
* @return false if no colors should be used
|
||||||
|
*/
|
||||||
|
static bool stderr_supports_colors(void)
|
||||||
|
{
|
||||||
|
const char *env_var;
|
||||||
|
const char *tmp;
|
||||||
|
|
||||||
|
if (isatty(2) != 1)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
env_var = getenv("TERM");
|
||||||
|
if (!env_var)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
tmp = strstr(env_var, "xterm");
|
||||||
|
if (!tmp)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reporting_init(enum reporting_color_mode mode)
|
||||||
|
{
|
||||||
|
switch (mode) {
|
||||||
|
case COLOR_MODE_COLOR:
|
||||||
|
reporting_use_color = true;
|
||||||
|
break;
|
||||||
|
case COLOR_MODE_COLOR_OFF:
|
||||||
|
reporting_use_color = false;
|
||||||
|
break;
|
||||||
|
default: /* Auto detect case and invalid settings */
|
||||||
|
reporting_use_color = stderr_supports_colors();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user