Skip to content

Commit a03c07c

Browse files
blucakeszybz
authored andcommitted
use LONG instead of BYTEs for binary fields
We hard-code little-endian byte sequences for binary fields, but that clearly doesn't work on big endian architectures like s390x. Switch to single LONG per 4-bytes field, so that the right thing happens automatically. Displaying notes found in: .note.package Owner Data size Description readelf: /usr/bin/bash: Warning: note with invalid namesz and/or descsz found at offset 0x0 readelf: /usr/bin/bash: Warning: type: 0x7e1afeca, namesize: 0x04000000, descsize: 0x78000000, alignment: 4
1 parent f8dbf42 commit a03c07c

8 files changed

Lines changed: 28 additions & 30 deletions

generate-package-notes.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,10 @@ def encode_bytes_lines(arr, prefix='', label='string'):
138138

139139
def encode_length(s, prefix='', label='string'):
140140
n = (len(s) + 1) * 4 // 4
141-
n1 = n % 0x100
142-
n2 = n // 0x100
143-
assert n2 < 0x100
144-
return prefix + encode_bytes([n1, n2, 0, 0]) + ' /* Length of {} including NUL */'.format(label)
141+
return f'{prefix}LONG(0x{n:04x}) /* Length of {label} including NUL */'
145142

146-
def encode_note_id(arr, prefix=''):
147-
assert len(arr) == 4
148-
return prefix + encode_bytes(arr) + ' /* Note ID */'
143+
def encode_note_id(id, prefix=''):
144+
return f'{prefix}LONG(0x{id:04x}) /* Note ID */'
149145

150146
def pad_string(s):
151147
return [0] * ((len(s) + 4) // 4 * 4 - len(s))
@@ -166,7 +162,7 @@ def encode_note(note_name, note_id, owner, value, readonly=True, prefix=''):
166162
l1, l2, l3, *l4, *l5,
167163
prefix + '}']
168164

169-
NOTE_ID = [0x7E, 0x1A, 0xFE, 0xCA]
165+
NOTE_ID= 0xcafe1a7e
170166

171167
def json_serialize(s):
172168
# Avoid taking space in the ELF header if there's no value to store

generate-package-notes.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,13 @@ write_script() {
253253

254254
printf 'SECTIONS\n{\n'
255255
printf ' .note.package %s: ALIGN(4) {\n' "${readonly}"
256-
printf ' BYTE(0x04) BYTE(0x00) BYTE(0x00) BYTE(0x00) /* Length of Owner including NUL */\n'
257-
printf ' BYTE(0x%02x) BYTE(0x%02x) BYTE(0x00) BYTE(0x00) /* Length of Value including NUL */\n' \
258-
$((value_len % 256)) $((value_len / 256))
256+
# Note that for the binary fields we use the native 4 bytes type, to avoid
257+
# endianness issues.
258+
printf ' LONG(0x0004) /* Length of Owner including NUL */\n'
259+
printf ' LONG(0x%04x) /* Length of Value including NUL */\n' \
260+
${value_len}
261+
printf ' LONG(0xcafe1a7e) /* Note ID */\n'
259262

260-
printf ' BYTE(0x7e) BYTE(0x1a) BYTE(0xfe) BYTE(0xca) /* Note ID */\n'
261263
printf " BYTE(0x46) BYTE(0x44) BYTE(0x4f) BYTE(0x00) /* Owner: 'FDO\\\\x00' */" # newline will be added by write_string
262264

263265
write_string "$1" ' ' 'Value' "$value_len"

tests/resources/fedora-cpe-os-release.ld

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
SECTIONS
22
{
33
.note.package (READONLY) : ALIGN(4) {
4-
BYTE(0x04) BYTE(0x00) BYTE(0x00) BYTE(0x00) /* Length of Owner including NUL */
5-
BYTE(0x38) BYTE(0x00) BYTE(0x00) BYTE(0x00) /* Length of Value including NUL */
6-
BYTE(0x7e) BYTE(0x1a) BYTE(0xfe) BYTE(0xca) /* Note ID */
4+
LONG(0x0004) /* Length of Owner including NUL */
5+
LONG(0x0038) /* Length of Value including NUL */
6+
LONG(0xcafe1a7e) /* Note ID */
77
BYTE(0x46) BYTE(0x44) BYTE(0x4f) BYTE(0x00) /* Owner: 'FDO\x00' */
88
BYTE(0x7b) BYTE(0x22) BYTE(0x74) BYTE(0x79) /* Value: '{"type":"rpm","osCpe":"cpe:/o:fedoraproject:fedora:34"}\x00' */
99
BYTE(0x70) BYTE(0x65) BYTE(0x22) BYTE(0x3a)

tests/resources/fedora-cpe-system-release.ld

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
SECTIONS
22
{
33
.note.package (READONLY) : ALIGN(4) {
4-
BYTE(0x04) BYTE(0x00) BYTE(0x00) BYTE(0x00) /* Length of Owner including NUL */
5-
BYTE(0x38) BYTE(0x00) BYTE(0x00) BYTE(0x00) /* Length of Value including NUL */
6-
BYTE(0x7e) BYTE(0x1a) BYTE(0xfe) BYTE(0xca) /* Note ID */
4+
LONG(0x0004) /* Length of Owner including NUL */
5+
LONG(0x0038) /* Length of Value including NUL */
6+
LONG(0xcafe1a7e) /* Note ID */
77
BYTE(0x46) BYTE(0x44) BYTE(0x4f) BYTE(0x00) /* Owner: 'FDO\x00' */
88
BYTE(0x7b) BYTE(0x22) BYTE(0x74) BYTE(0x79) /* Value: '{"type":"rpm","osCpe":"cpe:/o:fedoraproject:fedora:33"}\x00' */
99
BYTE(0x70) BYTE(0x65) BYTE(0x22) BYTE(0x3a)

tests/resources/fedora-long-name.ld

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
SECTIONS
22
{
33
.note.package (READONLY) : ALIGN(4) {
4-
BYTE(0x04) BYTE(0x00) BYTE(0x00) BYTE(0x00) /* Length of Owner including NUL */
5-
BYTE(0x33) BYTE(0x01) BYTE(0x00) BYTE(0x00) /* Length of Value including NUL */
6-
BYTE(0x7e) BYTE(0x1a) BYTE(0xfe) BYTE(0xca) /* Note ID */
4+
LONG(0x0004) /* Length of Owner including NUL */
5+
LONG(0x0133) /* Length of Value including NUL */
6+
LONG(0xcafe1a7e) /* Note ID */
77
BYTE(0x46) BYTE(0x44) BYTE(0x4f) BYTE(0x00) /* Owner: 'FDO\x00' */
88
BYTE(0x7b) BYTE(0x22) BYTE(0x74) BYTE(0x79) /* Value: '{"type":"rpm","name":"rust-plist+enable_unstable_features_that_may_break_with_minor_version_bumps-devel","version":"200:1.3.1~rc1.post2^final3","architecture":"ppc64le","osCpe":"cpe:/o:fedoraproject:fedora:35","debugInfoUrl":"https://somewhere.on.the.internet.there.is.a.server.which.is.never.wrong/query"}\x00\x00' */
99
BYTE(0x70) BYTE(0x65) BYTE(0x22) BYTE(0x3a)

tests/resources/fedora-package.ld

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
SECTIONS
22
{
33
.note.package (READONLY) : ALIGN(4) {
4-
BYTE(0x04) BYTE(0x00) BYTE(0x00) BYTE(0x00) /* Length of Owner including NUL */
5-
BYTE(0x58) BYTE(0x00) BYTE(0x00) BYTE(0x00) /* Length of Value including NUL */
6-
BYTE(0x7e) BYTE(0x1a) BYTE(0xfe) BYTE(0xca) /* Note ID */
4+
LONG(0x0004) /* Length of Owner including NUL */
5+
LONG(0x0058) /* Length of Value including NUL */
6+
LONG(0xcafe1a7e) /* Note ID */
77
BYTE(0x46) BYTE(0x44) BYTE(0x4f) BYTE(0x00) /* Owner: 'FDO\x00' */
88
BYTE(0x7b) BYTE(0x22) BYTE(0x74) BYTE(0x79) /* Value: '{"type":"rpm","name":"package","version":"1.2.3","architecture":"noarch","osCpe":"CPE"}\x00' */
99
BYTE(0x70) BYTE(0x65) BYTE(0x22) BYTE(0x3a)

tests/resources/very-short-rw.ld

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
SECTIONS
22
{
33
.note.package : ALIGN(4) {
4-
BYTE(0x04) BYTE(0x00) BYTE(0x00) BYTE(0x00) /* Length of Owner including NUL */
5-
BYTE(0x47) BYTE(0x00) BYTE(0x00) BYTE(0x00) /* Length of Value including NUL */
6-
BYTE(0x7e) BYTE(0x1a) BYTE(0xfe) BYTE(0xca) /* Note ID */
4+
LONG(0x0004) /* Length of Owner including NUL */
5+
LONG(0x0047) /* Length of Value including NUL */
6+
LONG(0xcafe1a7e) /* Note ID */
77
BYTE(0x46) BYTE(0x44) BYTE(0x4f) BYTE(0x00) /* Owner: 'FDO\x00' */
88
BYTE(0x7b) BYTE(0x22) BYTE(0x74) BYTE(0x79) /* Value: '{"type":"deb","name":"A","version":"0","architecture":"x","osCpe":"o"}\x00\x00' */
99
BYTE(0x70) BYTE(0x65) BYTE(0x22) BYTE(0x3a)

tests/resources/very-short.ld

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
SECTIONS
22
{
33
.note.package (READONLY) : ALIGN(4) {
4-
BYTE(0x04) BYTE(0x00) BYTE(0x00) BYTE(0x00) /* Length of Owner including NUL */
5-
BYTE(0x47) BYTE(0x00) BYTE(0x00) BYTE(0x00) /* Length of Value including NUL */
6-
BYTE(0x7e) BYTE(0x1a) BYTE(0xfe) BYTE(0xca) /* Note ID */
4+
LONG(0x0004) /* Length of Owner including NUL */
5+
LONG(0x0047) /* Length of Value including NUL */
6+
LONG(0xcafe1a7e) /* Note ID */
77
BYTE(0x46) BYTE(0x44) BYTE(0x4f) BYTE(0x00) /* Owner: 'FDO\x00' */
88
BYTE(0x7b) BYTE(0x22) BYTE(0x74) BYTE(0x79) /* Value: '{"type":"deb","name":"A","version":"0","architecture":"x","osCpe":"o"}\x00\x00' */
99
BYTE(0x70) BYTE(0x65) BYTE(0x22) BYTE(0x3a)

0 commit comments

Comments
 (0)