diff --git a/cough/file.py b/cough/file.py
index 84338e7..d96aa8a 100644
--- a/cough/file.py
+++ b/cough/file.py
@@ -125,25 +125,17 @@ class ObjectModule:
         return bytes(body_buffer)
 
     def dump_sections(self):
+        data_buf_offset = FileHeader.struct.size + 40 * len(self.sections)
+        hdrs_buf = bytearray()
         data_buf = bytearray()
-        sections_offsets = []
-        reloc_offsets = []
         for sec in self.sections:
             if sec.data:
-                sections_offsets.append(len(data_buf))
+                sec.pointer_to_raw_data = data_buf_offset + len(data_buf)
                 data_buf += sec.data
             if sec.relocations:
-                reloc_offsets.append(len(data_buf))
+                sec.pointer_to_relocations = data_buf_offset + len(data_buf)
                 for reloc in sec.relocations:
                     data_buf += reloc.pack()
+            hdrs_buf += sec.get_header()
 
-        sections_buffer_offset = FileHeader.struct.size + 40 * len(self.sections)
-        hdrs_and_data_buf = bytearray()
-        for i, sec in enumerate(self.sections):
-            if sec.data:
-                sec.pointer_to_raw_data = sections_buffer_offset + sections_offsets[i]
-            if sec.relocations:
-                sec.pointer_to_relocations = sections_buffer_offset + reloc_offsets[i]
-            hdrs_and_data_buf += sec.get_header()
-        hdrs_and_data_buf += data_buf
-        return bytes(hdrs_and_data_buf)
+        return bytes(hdrs_buf + data_buf)

Exception when adding an uninitialized section before an initialized section
file.py:133 is inconsistent with file.py:144
diff --git a/tests/test_coff.py b/tests/test_coff.py
index 8ca9ce4..2193214 100644
--- a/tests/test_coff.py
+++ b/tests/test_coff.py
@@ -63,3 +63,16 @@ def test_reloc():
     subprocess.run(['PowerShell.exe', BUILD_SCRIPT, file.name, '/out:' + '"' + exe_path + '"'], check=True)
     proc = subprocess.run([exe_path], stdout=subprocess.PIPE, check=True)
     assert proc.stdout == b'A'
+
+
+def test_uninit_before_init():
+    module = cough.ObjectModule()
+
+    sec_uninit = cough.Section(b'uninit', cough.SectionFlags.CNT_UNINITIALIZED_DATA)
+    sec_uninit.size_of_raw_data = 0x400
+    module.sections.append(sec_uninit)
+
+    sec_init = cough.Section(b'init', 0, b'\xAA\xBB\xCC\xDD')
+    module.sections.append(sec_init)
+
+    assert module.get_buffer()

pip install -e .
parse_log_pytest
requirements.txt
pytest
apt-get update apt-get install -y gcc
3.9
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
-e git+https://github.com/d3dave/cough.git@b2a4ebe62f953e35beff00a9a7d7426cc20f6350#egg=cough
exceptiongroup==1.2.2
iniconfig==2.1.0
packaging==24.2
pluggy==1.5.0
pytest==8.3.5
tomli==2.2.1

name: cough
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - exceptiongroup==1.2.2
      - iniconfig==2.1.0
      - packaging==24.2
      - pluggy==1.5.0
      - pytest==8.3.5
      - tomli==2.2.1
prefix: /opt/conda/envs/cough

tests/test_coff.py::test_uninit_before_init
tests/test_coff.py::test_coff tests/test_coff.py::test_reloc
cough/file.py
cough/file.py