[nasm:macho-unify] output: macho -- Use lookup_known_section helper

nasm-bot for Cyrill Gorcunov gorcunov at gmail.com
Tue Jun 30 17:03:01 PDT 2020


Commit-ID:  11afb191c54764ddedd874118ed2e3f66b3483c2
Gitweb:     http://repo.or.cz/w/nasm.git?a=commitdiff;h=11afb191c54764ddedd874118ed2e3f66b3483c2
Author:     Cyrill Gorcunov <gorcunov at gmail.com>
AuthorDate: Sat, 5 May 2018 12:05:44 +0300
Committer:  Cyrill Gorcunov <gorcunov at gmail.com>
CommitDate: Wed, 9 May 2018 18:03:50 +0300

output: macho -- Use lookup_known_section helper

To make it similar to elf code and eliminate code duplication.

Signed-off-by: Cyrill Gorcunov <gorcunov at gmail.com>


---
 output/outmacho.c | 59 +++++++++++++++++++++++++++++++++----------------------
 1 file changed, 35 insertions(+), 24 deletions(-)

diff --git a/output/outmacho.c b/output/outmacho.c
index 34385fa7..84a083dd 100644
--- a/output/outmacho.c
+++ b/output/outmacho.c
@@ -706,12 +706,12 @@ static void macho_output(int32_t secto, const void *data,
 #define NO_TYPE S_NASM_TYPE_MASK
 
 /* Translation table from traditional Unix section names to Mach-O */
-static const struct sectmap {
+static const struct macho_known_section {
     const char      *nasmsect;
     const char      *segname;
     const char      *sectname;
     const uint32_t  flags;
-} sectmap[] = {
+} known_sections[] = {
     { ".text",          "__TEXT",   "__text",           S_CODE          },
     { ".data",          "__DATA",   "__data",           S_REGULAR       },
     { ".rodata",        "__DATA",   "__const",          S_REGULAR       },
@@ -720,7 +720,6 @@ static const struct sectmap {
     { ".debug_info",    "__DWARF",  "__debug_info",     S_ATTR_DEBUG    },
     { ".debug_line",    "__DWARF",  "__debug_line",     S_ATTR_DEBUG    },
     { ".debug_str",     "__DWARF",  "__debug_str",      S_ATTR_DEBUG    },
-    { NULL, NULL, NULL, 0 }
 };
 
 /* Section type or attribute directives */
@@ -740,10 +739,28 @@ static const struct sect_attribs {
     { NULL, 0 }
 };
 
+static const struct macho_known_section *
+lookup_known_section(const char *name, bool by_sectname)
+{
+    size_t i;
+
+    if (name && name[0]) {
+            for (i = 0; i < ARRAY_SIZE(known_sections); i++) {
+                const char *p = by_sectname ?
+                    known_sections[i].sectname :
+                    known_sections[i].nasmsect;
+                if (!strcmp(name, p))
+                    return &known_sections[i];
+            }
+    }
+
+    return NULL;
+}
+
 static int32_t macho_section(char *name, int pass, int *bits)
 {
+    const struct macho_known_section *known_section;
     char *sectionAttributes;
-    const struct sectmap *sm;
     struct section *s;
     const char *section, *segment;
     uint32_t flags;
@@ -790,29 +807,23 @@ static int32_t macho_section(char *name, int pass, int *bits)
 	    nasm_error(ERR_NONFATAL, "section name %s too long\n", section);
 	}
 
-	if (!strcmp(section, "__text")) {
-	    flags = S_REGULAR | S_ATTR_SOME_INSTRUCTIONS |
-		S_ATTR_PURE_INSTRUCTIONS;
-	} else if (!strcmp(section, "__bss")) {
-	    flags = S_ZEROFILL;
-	} else {
-	    flags = S_REGULAR;
-	}
+        known_section = lookup_known_section(section, true);
+        if (known_section)
+            flags = known_section->flags;
+        else
+            flags = S_REGULAR;
     } else {
-	for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
-	    /* make lookup into section name translation table */
-	    if (!strcmp(name, sm->nasmsect)) {
-		segment = sm->segname;
-		section = sm->sectname;
-		flags = sm->flags;
-		goto found;
-	    }
-	}
-	nasm_error(ERR_NONFATAL, "unknown section name\n");
-	return NO_SEG;
+        known_section = lookup_known_section(name, false);
+        if (!known_section) {
+            nasm_error(ERR_NONFATAL, "unknown section name %s\n", name);
+            return NO_SEG;
+        }
+
+        segment = known_section->segname;
+        section = known_section->sectname;
+        flags = known_section->flags;
     }
 
- found:
     /* try to find section with that name */
     s = get_section_by_name(segment, section);
 


More information about the Nasm-commits mailing list