]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ukify: Prefer using llvm-objcopy instead of objcopy
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 19 Dec 2022 14:29:43 +0000 (15:29 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 22 Dec 2022 11:20:24 +0000 (12:20 +0100)
llvm-objcopy works on stubs built for foreign architectures whereas
objcopy doesn't so let's prefer using llvm-objcopy instead of objcopy.

llvm-objcopy automatically sets the virtual address and doesn't provide
an option to set it manually so we only add --change-section-vma when
using objcopy

The default section flags differ between llvm-objcopy and objcopy
so we add a default for the section flags so we make sure all sections
are read-only data unless specified otherwise.

src/ukify/ukify.py

index 80a4e9c9cabe3079f5a6c96f9dc15f00ce463f06..63e7202d3dafb854ec16d15c854a62a01f604e70 100755 (executable)
@@ -200,12 +200,12 @@ class Section:
     name: str
     content: pathlib.Path
     tmpfile: typing.IO | None = None
-    flags: list[str] | None = dataclasses.field(default=None)
+    flags: list[str] = dataclasses.field(default_factory=lambda: ['data', 'readonly'])
     offset: int | None = None
     measure: bool = False
 
     @classmethod
-    def create(cls, name, contents, flags=None, measure=False):
+    def create(cls, name, contents, **kwargs):
         if isinstance(contents, str | bytes):
             mode = 'wt' if isinstance(contents, str) else 'wb'
             tmp = tempfile.NamedTemporaryFile(mode=mode, prefix=f'tmp{name}')
@@ -215,7 +215,7 @@ class Section:
         else:
             tmp = None
 
-        return cls(name, contents, tmpfile=tmp, flags=flags, measure=measure)
+        return cls(name, contents, tmpfile=tmp, **kwargs)
 
     @classmethod
     def parse_arg(cls, s):
@@ -521,21 +521,22 @@ def make_uki(opts):
     else:
         output = opts.output
 
-    objcopy_tool = find_tool('objcopy', opts=opts)
+    objcopy_tool = find_tool('llvm-objcopy', 'objcopy', opts=opts)
 
     cmd = [
         objcopy_tool,
         opts.stub,
         *itertools.chain.from_iterable(
-            ('--add-section',        f'{s.name}={s.content}',
-             '--change-section-vma', f'{s.name}=0x{s.offset:x}')
+            ('--add-section',       f'{s.name}={s.content}',
+             '--set-section-flags', f"{s.name}={','.join(s.flags)}")
             for s in uki.sections),
-        *itertools.chain.from_iterable(
-            ('--set-section-flags',  f"{s.name}={','.join(s.flags)}")
-            for s in uki.sections
-            if s.flags is not None),
         output,
     ]
+
+    if pathlib.Path(objcopy_tool).name != 'llvm-objcopy':
+        cmd += itertools.chain.from_iterable(
+            ('--change-section-vma', f'{s.name}=0x{s.offset:x}') for s in uki.sections)
+
     print('+', shell_join(cmd))
     subprocess.check_call(cmd)