From 1002a7c5901cdb185711fc197b4b0c6cee1de91d Mon Sep 17 00:00:00 2001
From: yury <jura@cp-lab.com>
Date: Thu, 20 Aug 2020 16:35:27 +0000
Subject: [PATCH] * 8086: Fixed very long section names when $HUGECODE is ON
 and section based smartlinking is used. * Added a utility function
 TrimStrCRC32().

git-svn-id: trunk@46511 -
---
 compiler/fpccrc.pas       | 21 ++++++++++++++++++++-
 compiler/ogomf.pas        |  4 ++--
 compiler/x86/agx86nsm.pas |  4 ++--
 3 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/compiler/fpccrc.pas b/compiler/fpccrc.pas
index a617b65dba..0e8249b0d5 100644
--- a/compiler/fpccrc.pas
+++ b/compiler/fpccrc.pas
@@ -26,7 +26,9 @@ Unit fpccrc;
 Interface
 
 Function UpdateCrc32(InitCrc:cardinal;const InBuf;InLen:integer):cardinal;
-
+{ If needed trims the string to maxlen, adding at the end the CRC32 of discarded chars.
+  The resulting string is guaranteed to be not longer than maxlen. }
+function TrimStrCRC32(const s: ansistring; maxlen: longint): ansistring;
 
 Implementation
 
@@ -73,4 +75,21 @@ begin
 end;
 
 
+function TrimStrCRC32(const s: ansistring; maxlen: longint): ansistring;
+var
+  crc: DWord;
+  len: longint;
+begin
+  len:=length(s);
+  if (len<=maxlen) or (len<12) then
+    result:=s
+  else
+   begin
+     dec(maxlen,11);
+     crc:=0;
+     crc:=UpdateCrc32(crc,s[maxlen+1],len-maxlen);
+     result:=copy(s,1,maxlen)+'$CRC'+hexstr(crc,8);
+   end;
+end;
+
 end.
diff --git a/compiler/ogomf.pas b/compiler/ogomf.pas
index b4c0047ca9..0755b0a2ec 100644
--- a/compiler/ogomf.pas
+++ b/compiler/ogomf.pas
@@ -803,7 +803,7 @@ implementation
 
     uses
        SysUtils,
-       cutils,verbose,globals,
+       cutils,verbose,globals,fpccrc,
        fmodule,aasmtai,aasmdata,
        ogmap,owomflib,elfbase,
        version
@@ -1053,7 +1053,7 @@ implementation
         if current_settings.x86memorymodel in x86_far_code_models then
           begin
             if cs_huge_code in current_settings.moduleswitches then
-              result:=aname + '_TEXT'
+              result:=TrimStrCRC32(aname,30) + '_TEXT'
             else
               result:=current_module.modulename^ + '_TEXT';
           end
diff --git a/compiler/x86/agx86nsm.pas b/compiler/x86/agx86nsm.pas
index a18979ea77..89b0fa8018 100644
--- a/compiler/x86/agx86nsm.pas
+++ b/compiler/x86/agx86nsm.pas
@@ -84,7 +84,7 @@ interface
   implementation
 
     uses
-      cutils,globals,systems,
+      cutils,globals,systems,fpccrc,
       fmodule,finput,verbose,cpuinfo,cgbase,omfbase
       ;
 
@@ -306,7 +306,7 @@ interface
         if current_settings.x86memorymodel in x86_far_code_models then
           begin
             if cs_huge_code in current_settings.moduleswitches then
-              result:=aname + '_TEXT'
+              result:=TrimStrCRC32(aname,30) + '_TEXT'
             else
               result:=current_module.modulename^ + '_TEXT';
           end