]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
elf_newdata: Make sure to set d_type to ELF_T_BYTE and d_align to one
authorMark Wielaard <mark@klomp.org>
Mon, 13 Apr 2026 12:29:27 +0000 (14:29 +0200)
committerMark Wielaard <mark@klomp.org>
Tue, 14 Apr 2026 00:06:49 +0000 (02:06 +0200)
If d_align isn't explicitly set in elf_newdata it will default to
zero. This confuses elf_update when more than one Elf_Data has been
added to a (new) Elf_Section. Only the last will appear to be added.
ELF_T_BYTE happens to be zero and so d_type did appear to be setup
correctly. Also set it explicitly to show the defaults.

The elf_newdata test happened to pass because it set d_align to 1
explicitly. But would fail if it wouldn't set it. Add some extra
checks.

Also update the elf_newdata man page with the new defaults.

* libelf/elf_newdata.c (elf_newdata): Explicitly set d_type
and d_align.
* tests/newdata.c (add_section_data): Check d_buf, d_size,
d_type, d_align and d_version default values.
* doc/elf_newdata.3: Document d_align defaults to one.

Signed-off-by: Mark Wielaard <mark@klomp.org>
doc/elf_newdata.3
libelf/elf_newdata.c
tests/newdata.c

index 0522bc15c2352543406dfe2efa181fcfb3923a7b..1530dfb7fbcdf3dd8a5cbbec5cadba94025a682e 100644 (file)
@@ -42,12 +42,11 @@ set to
 .I d_version
 set to
 .BR EV_CURRENT ,
-and
 .IR d_size ,
 .IR d_off ,
-and
+set to zero, and
 .IR d_align
-set to zero.
+set to one.
 
 .SH PARAMETERS
 .TP
index 0063d599180fcd61647fe958d4624b8aee363cd1..b59b87ecfc0ae4782024b9933d9d97abad67aaa8 100644 (file)
@@ -118,6 +118,8 @@ elf_newdata (Elf_Scn *scn)
 
   /* Set the predefined values.  */
   result->data.d.d_version = EV_CURRENT;
+  result->data.d.d_type = ELF_T_BYTE;
+  result->data.d.d_align = 1;
 
   result->data.s = scn;
 
index fcf26acf8cb916467fd488fe17cd64b5327b561c..5b7da8d61b146f8eeee5e6f40a176022e8ffc9cb 100644 (file)
@@ -54,11 +54,38 @@ add_section_data (Elf *elf, char *buf, size_t len)
       exit (1);
     }
 
+  if (data->d_buf != NULL)
+    {
+      printf ("newdata d_buf isn't NULL\n");
+      exit (1);
+    }
+
+  if (data->d_size != 0)
+    {
+      printf ("newdata d_size isn't 0\n");
+      exit (1);
+    }
+
+  if (data->d_type != ELF_T_BYTE)
+    {
+      printf ("newdata d_type isn't ELF_T_BYTE\n");
+      exit (1);
+    }
+
+  if (data->d_align != 1)
+    {
+      printf ("newdata d_align isn't 1\n");
+      exit (1);
+    }
+
+  if (data->d_version != EV_CURRENT)
+    {
+      printf ("newdata d_version isn't EV_CURRENT\n");
+      exit (1);
+    }
+
   data->d_buf = buf;
-  data->d_type = ELF_T_BYTE;
   data->d_size = len;
-  data->d_align = 1;
-  data->d_version = EV_CURRENT;
 
   // Let the library compute the internal structure information.
   if (elf_update (elf, ELF_C_NULL) < 0)