From 69ad1aeffe334f430a6faf106c2425d6c67979f7 Mon Sep 17 00:00:00 2001 From: Anton Kochkov Date: Tue, 14 Apr 2020 01:55:00 +0800 Subject: [PATCH] Fix R_PACKED() macro usage (#16571) --- DEVELOPERS.md | 20 ++++++++++++++++++++ libr/bin/format/le/le_specs.h | 2 +- libr/util/protobuf.c | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/DEVELOPERS.md b/DEVELOPERS.md index bdeb95a6e8..0964a14413 100644 --- a/DEVELOPERS.md +++ b/DEVELOPERS.md @@ -331,6 +331,26 @@ You may use directory-local variables by putting into `.dir-locals.el`. +## Packed structures + +Due to the various differences between platforms and compilers radare2 +has a special helper macro - `R_PACKED()`. Instead of non-portable +`#pragma pack` or `__attribute__((packed))` it is advised to use this macro +instead. To wrap the code inside of it you just need to write: +```c +R_PACKED (union mystruct { + int a; + char b; +}) +``` +or in case of typedef: +```c +R_PACKED (typedef structmystruct { + int a; + char b; +}) +``` + ## Modules The radare2 code base is modularized into different libraries that are diff --git a/libr/bin/format/le/le_specs.h b/libr/bin/format/le/le_specs.h index 6006eef206..74444b6856 100644 --- a/libr/bin/format/le/le_specs.h +++ b/libr/bin/format/le/le_specs.h @@ -47,7 +47,7 @@ typedef struct LE_entry_bundle_header_s { #define ENTRY_EXPORTED 0x01 #define ENTRY_PARAM_COUNT_MASK 0xF8 -typedef R_PACKED (union LE_entry_bundle_entry_u { +R_PACKED (typedef union LE_entry_bundle_entry_u { R_PACKED (struct { ut8 flags; // First bit set if exported, mask with 0xF8 to get parameters count ut16 offset; // This is the offset in the object for the entry point defined at this ordinal number. diff --git a/libr/util/protobuf.c b/libr/util/protobuf.c index c3b03cfe8b..104902e611 100644 --- a/libr/util/protobuf.c +++ b/libr/util/protobuf.c @@ -13,7 +13,7 @@ typedef double ft64; #define WIRE_END_GRP 4 // groups (deprecated) #define WIRE_32_BIT 5 // fixed32, sfixed32, float -typedef R_PACKED (struct _proto_head { +R_PACKED (typedef struct _proto_head { ut8 wire : 3; ut8 number : 5; }) proto_head_t;