From 05d21996ba7b8cb7c8708ad8f7a6678d406e0936 Mon Sep 17 00:00:00 2001
From: Chih-Hung Hsieh <chh@google.com>
Date: Sun, 31 May 2020 14:03:51 +0100
Subject: [PATCH 2/3] Replace libdwfl nested functions with macros, part 1.

Prepare to compile with clang.
---
 libdwfl/elf-from-memory.c | 77 +++++++++++++++++++++------------------
 1 file changed, 42 insertions(+), 35 deletions(-)

diff --git a/libdwfl/elf-from-memory.c b/libdwfl/elf-from-memory.c
index c54c1b9..143e124 100644
--- a/libdwfl/elf-from-memory.c
+++ b/libdwfl/elf-from-memory.c
@@ -229,29 +229,33 @@ elf_from_remote_memory (GElf_Addr ehdr_vma,
 	 segments_end, segments_end_mem and loadbase (if not
 	 found_base yet).  Returns true if sanity checking failed,
 	 false otherwise.  */
-      inline bool handle_segment (GElf_Addr vaddr, GElf_Off offset,
-				  GElf_Xword filesz, GElf_Xword memsz)
-	{
-	  /* Sanity check the segment load aligns with the pagesize.  */
-	  if (((vaddr - offset) & (pagesize - 1)) != 0)
-	    return true;
-
-	  GElf_Off segment_end = ((offset + filesz + pagesize - 1)
-				  & -pagesize);
-
-	  if (segment_end > (GElf_Off) contents_size)
-	    contents_size = segment_end;
-
-	  if (!found_base && (offset & -pagesize) == 0)
-	    {
-	      loadbase = ehdr_vma - (vaddr & -pagesize);
-	      found_base = true;
-	    }
-
-	  segments_end = offset + filesz;
-	  segments_end_mem = offset + memsz;
-	  return false;
-	}
+      #define handle_segment(_vaddr, _offset, _filesz, _memsz) \
+    ( { \
+      bool result; \
+      GElf_Addr vaddr = _vaddr; \
+      GElf_Off offset = _offset; \
+      GElf_Xword filesz = _filesz; \
+      GElf_Xword memsz = _memsz; \
+      /* Sanity check the segment load aligns with the pagesize.  */ \
+      if (((vaddr - offset) & (pagesize - 1)) != 0) \
+        result = true; \
+      else \
+        { \
+          result = false; \
+          GElf_Off segment_end = ((offset + filesz + pagesize - 1) \
+                                 & -pagesize); \
+          if (segment_end > (GElf_Off) contents_size) \
+            contents_size = segment_end; \
+          if (!found_base && (offset & -pagesize) == 0) \
+            { \
+              loadbase = ehdr_vma - (vaddr & -pagesize); \
+              found_base = true; \
+            } \
+          segments_end = offset + filesz; \
+          segments_end_mem = offset + memsz; \
+        } \
+      result; \
+    } )
 
     case ELFCLASS32:
       if (elf32_xlatetom (&xlateto, &xlatefrom,
@@ -309,18 +313,21 @@ elf_from_remote_memory (GElf_Addr ehdr_vma,
     {
       /* Reads the given segment.  Returns true if reading fails,
 	 false otherwise.  */
-      inline bool handle_segment (GElf_Addr vaddr, GElf_Off offset,
-				  GElf_Xword filesz)
-	{
-	  GElf_Off start = offset & -pagesize;
-	  GElf_Off end = (offset + filesz + pagesize - 1) & -pagesize;
-	  if (end > (GElf_Off) contents_size)
-	    end = contents_size;
-	  nread = (*read_memory) (arg, buffer + start,
-				  (loadbase + vaddr) & -pagesize,
-				  end - start, end - start);
-	  return nread <= 0;
-	}
+      #undef handle_segment
+      #define handle_segment(_vaddr, _offset, _filesz) \
+        ( { \
+          GElf_Addr vaddr = _vaddr; \
+          GElf_Off offset = _offset; \
+          GElf_Xword filesz = _filesz; \
+          GElf_Off start = offset & -pagesize; \
+          GElf_Off end = (offset + filesz + pagesize - 1) & -pagesize; \
+          if (end > (GElf_Off) contents_size) \
+            end = contents_size; \
+          nread = (*read_memory) (arg, buffer + start, \
+                                  (loadbase + vaddr) & -pagesize, \
+                                   end - start, end - start); \
+          (nread <= 0); \
+        } )
 
     case ELFCLASS32:
       for (uint_fast16_t i = 0; i < phnum; ++i)
-- 
2.26.2

